My Technical Notes

Tuesday, 4 June 2013

What is wrong with the following Regular Expression?


// following regex looks for 5 to 11 digits
bool isMatch = Regex.IsMatch("123456", "[0-9]{5, 11}");
// isMatch is false

?

The answer is there is a space between the comma and "11" in the regular expression string! The following code works:


// following regex looks for 5 to 11 digits
bool isMatch = Regex.IsMatch("123456", "[0-9]{5,11}"); // removed the space between , and 11
// isMatch is correctly set to true

No comments: