Regex expression for string pattern to start with an alphapbet and not a number

I would like to validate if the text entered by the user
1) contains only alphanumeric and / or underscore
2) starts with an alphabet and not a number
3) contains only max. 50 characters.

I have the following Regex expression that does 1 and 3 but how do I add condition 2, ie text should start with an alphabet to the Regex pattern?

  Regex pattern = new Regex("^[a-zA-Z0-9_]{1,50}$");

Please help.
Thanks in advance!
VD1234Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Richard QuadlingSenior Software DeveloperCommented:
^[a-z][a-z0-9_]{,50}$

0
Richard QuadlingSenior Software DeveloperCommented:


/^[a-z][a-z0-9_]{,50}$/i

Options: case insensitive; ^ and $ match at line breaks

Assert position at the beginning of a line (at beginning of the string or after a line break character) «^»
Match a single character in the range between a and z «[a-z]»
Match a single character present in the list below «[a-z0-9_]»
   A character in the range between a and z «a-z»
   A character in the range between 0 and 9 «0-9»
   The character _ «_»
Match the characters {,50} literally «{,50}»
Assert position at the end of a line (at the end of the string or before a line break character) «$»


Created with RegexBuddy
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Richard QuadlingSenior Software DeveloperCommented:
OOps,

Make that {0,50}



^[a-z][a-z0-9_]{0,50}$

Options: case insensitive; ^ and $ match at line breaks

Assert position at the beginning of a line (at beginning of the string or after a line break character) «^»
Match a single character in the range between a and z «[a-z]»
Match a single character present in the list below «[a-z0-9_]{0,50}»
   Between zero and 50 times, as many times as possible, giving back as needed (greedy) «{0,50}»
   A character in the range between a and z «a-z»
   A character in the range between 0 and 9 «0-9»
   The character _ «_»
Assert position at the end of a line (at the end of the string or before a line break character) «$»


Created with RegexBuddy
0
Cloud Class® Course: Python 3 Fundamentals

This course will teach participants about installing and configuring Python, syntax, importing, statements, types, strings, booleans, files, lists, tuples, comprehensions, functions, and classes.

anna_arunCommented:
^[a-zA-Z][0-9a-zA-Z]{1,8}$

Try this out. It will work.
[a-zA-Z] will check that the first character is always an alphabet
[0-9a-zA-Z] will check for alphanumeric (Because of $ in teh end, it will check till end of string)
{1,8} specify the minimum and maximum length of characters allowed respectively. You can change these as per your requirement.
0
Richard QuadlingSenior Software DeveloperCommented:
Here is the output of RegexBuddy to "Test if the regex matches a string entirely" using C#.


bool foundMatch = false;
try {
	foundMatch = Regex.IsMatch(subjectString, "^[a-z][a-z0-9_]{0,50}$", RegexOptions.Multiline);
} catch (ArgumentException ex) {
	// Syntax error in the regular expression
}

Open in new window

0
Richard QuadlingSenior Software DeveloperCommented:
Sorry. Missed case insensitivity.


bool foundMatch = false;
try {
	foundMatch = Regex.IsMatch(subjectString, "^[a-z][a-z0-9_]{0,50}$", RegexOptions.IgnoreCase | RegexOptions.Multiline);
} catch (ArgumentException ex) {
	// Syntax error in the regular expression
}

Open in new window

0
VD1234Author Commented:
For the above expression all the strings evaluate to false. Even valid text like ad1 evaluates to false for the pattern match.

any thoughts?
0
Richard QuadlingSenior Software DeveloperCommented:
A PHP script to show things work.

Outputs ...

Does ad1 match : True
Does 99 Red Balloons match : False
Does BalloonsRed99 match : True
<?php
$a_Strings = array
	(
	'ad1',
	'99 Red Balloons',
	'BalloonsRed99',
	);
foreach($a_Strings as $s_String)
	{
	echo
		"Does $s_String match : ",
		(preg_match('/^[a-z][a-z0-9_]{0,50}$/i', $s_String) ? 'True' : 'False'),
		PHP_EOL;
	}
?>

Open in new window

0
VD1234Author Commented:
 Regex pattern = new Regex("^[a-z][a-z0-9_]{1,50}$/i");

            if (pattern.IsMatch(strFieldName) == false)
            {
//messgae that pattern didnot match
}

In the above code always the expression evaluates to false.

Even when the string is something like abcd
0
ozoCommented:
Regex pattern = new Regex("/^[a-z][a-z0-9_]{1,50}$/i");
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Regular Expressions

From novice to tech pro — start learning today.