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!
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!
^[a-z][a-z0-9_]{,50}$
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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
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
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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
}
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
}
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?
any thoughts?
A PHP script to show things work.
Outputs ...
Does ad1 match : True
Does 99 Red Balloons match : False
Does BalloonsRed99 match : True
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;
}
?>
ASKER
Regex pattern = new Regex("^[a-z][a-z0-9_]{1,5 0}$/i");
if (pattern.IsMatch(strFieldN ame) == false)
{
//messgae that pattern didnot match
}
In the above code always the expression evaluates to false.
Even when the string is something like abcd
if (pattern.IsMatch(strFieldN
{
//messgae that pattern didnot match
}
In the above code always the expression evaluates to false.
Even when the string is something like abcd
Regex pattern = new Regex("/^[a-z][a-z0-9_]{1, 50}$/i");