Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Regex 11 digit string capturing

Tags:

c#

regex

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.

like image 253
Kunal Deo Avatar asked Dec 21 '22 03:12

Kunal Deo


1 Answers

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}";
like image 134
Shiplu Mokaddim Avatar answered Dec 24 '22 00:12

Shiplu Mokaddim