Link to home
Start Free TrialLog in
Avatar of VD1234
VD1234

asked on

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!
Avatar of Richard Quadling
Richard Quadling
Flag of United Kingdom of Great Britain and Northern Ireland image

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

ASKER CERTIFIED SOLUTION
Avatar of Richard Quadling
Richard Quadling
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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

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

Avatar of VD1234
VD1234

ASKER

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

any thoughts?
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

Avatar of VD1234

ASKER

 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
Avatar of ozo
Regex pattern = new Regex("/^[a-z][a-z0-9_]{1,50}$/i");