• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1246
  • Last Modified:

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!
0
VD1234
Asked:
VD1234
2 Solutions
 
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
 
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
Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

 
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

Featured Post

Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now