String pattern = @"^(\d{11})$";
String input = "You number is:11126564312 and 12234322121 \n\n23211212345";
Match match = Regex.Match(input,pattern);
From the above code I am planning to capture the 11 digit strings present in above text but match.Success is always returning false. Any ideas.
This is because you have used ^
and $
.
Explaination: The meaning of your regular expression is "match any string that contains exactly 11 digits from start to end". The string You number is:11126564312 and 12234322121 \n\n23211212345
is not a string like that. 01234567890
is like that string.
What you need: You need regular expression for match any string that contains exactly 11 digits. start to end is omitted. ^
and $
is used for this. So you need this regex.
String pattern = @"(\d{11})";
As the sub-pattern to capture contains the whole regex you dont need ()
at all. Just the regex ill do.
String pattern = @"\d{11}";
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With