We help IT Professionals succeed at work.

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

VD1234
VD1234 asked
on
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!
Comment
Watch Question

Richard QuadlingSenior Software Developer
CERTIFIED EXPERT

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

Senior Software Developer
CERTIFIED EXPERT
Commented:


/^[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
Richard QuadlingSenior Software Developer
CERTIFIED EXPERT

Commented:
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
^[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.
Richard QuadlingSenior Software Developer
CERTIFIED EXPERT

Commented:
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

Richard QuadlingSenior Software Developer
CERTIFIED EXPERT

Commented:
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

Author

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?
Richard QuadlingSenior Software Developer
CERTIFIED EXPERT

Commented:
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

Author

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
ozo
CERTIFIED EXPERT
Most Valuable Expert 2014
Top Expert 2015

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

Explore More ContentExplore courses, solutions, and other research materials related to this topic.