I am using VS's built-in RegexValitor to check my textbox control input which is a string of specific numbers. String length is undefined (no maximum symbols value), it may be empty.
The string should contain numbers 2 to 5 in a series, no spaces("spacebar") or letters.
Example strings:
225553344555
234522455555555
Which regex expression should I use for this case?
Many thanks!
CodePudding user response:
I am not absolutely sure to have understood your requirements, but I suggest
^[2-5]*$
Which stands for "any character between 2 and 5 (included), 0 times or more"
You can test it on https://regex101.com/r/UNq7AO/1
CodePudding user response:
You can use "^[2-5] $"
pattern to achieve what you desire.
Regex regex = new Regex("^[2-5] $");
string s1 = "225553344555";
string s2 = "234522455555555";
string s3 = "1775224559898";
bool b1 = regex.IsMatch(s1); // True
bool b2 = regex.IsMatch(s2); // True
bool b3 = regex.IsMatch(s3); // False