I have a written a Regex to check whether the string consists of 9 digits or not as follows but this always returning me false even if I have my string as 123456789.
if (Regex.IsMatch(strSSN, " ^\\d{9}$"))
Can any one tell what’s wrong and also if any better one provide me .. What i am achieving is the string should have only numeric data and the length should be =9
Spaces are significant in regular expressions (and of course, you can’t match a space character before the start of the string). Remove it and everything should be fine:
Also, you generally want to use verbatim strings for regexes in C#, so you don’t have to double your backslashes, but this is just a convenience, not the reason for your problem: