Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Regex to match all us phone number formats

Tags:

First of all i would say i have seen many example here and googled but none found that matches all the condition i am looking for some match top 3 not below some inbetween. Kindly let me know how to put all of them in one place.

(xxx)xxxxxxx (xxx) xxxxxxx (xxx)xxx-xxxx (xxx) xxx-xxxx xxxxxxxxxx xxx-xxx-xxxxx 

Using as :

  const string MatchPhonePattern =            @"\(?\d{3}\)?-? *\d{3}-? *-?\d{4}";             Regex rx = new Regex(MatchPhonePattern, RegexOptions.Compiled | RegexOptions.IgnoreCase);             // Find matches.             MatchCollection matches = rx.Matches(text);             // Report the number of matches found.             int noOfMatches = matches.Count;             // Report on each match.              foreach (Match match in matches)             {                  tempPhoneNumbers= match.Value.ToString(); ;               } 

SAMPLE OUTPUT:

3087774825 (281)388-0388 (281)388-0300 (979) 778-0978 (281)934-2479 (281)934-2447 (979)826-3273 (979)826-3255 1334714149 (281)356-2530 (281)356-5264 (936)825-2081 (832)595-9500 (832)595-9501 281-342-2452 1334431660 
like image 438
confusedMind Avatar asked Aug 06 '13 22:08

confusedMind


People also ask

How do you write regex in a phone number?

Regular expression to allow numbers like +111 123 456 789: ^(\\+\\d{1,3}( )?)?(\\d{3}[ ]?){2}\\d{3}$

How can I check mobile number in regex?

/^([+]\d{2})? \d{10}$/ This is how this regex for mobile number is working. + sign is used for world wide matching of number.

How do I validate a phone number format?

EPP-style phone numbers use the format + CCC . NNNNNNNNNN x EEEE , where C is the 1–3 digit country code, N is up to 14 digits, and E is the (optional) extension. The leading plus sign and the dot following the country code are required. The literal “x” character is required only if an extension is provided.

What does '$' mean in regex?

$ means "Match the end of the string" (the position after the last character in the string).


2 Answers

\(?\d{3}\)?-? *\d{3}-? *-?\d{4}

like image 56
FlyingStreudel Avatar answered Oct 15 '22 14:10

FlyingStreudel


 public bool IsValidPhone(string Phone)     {         try         {             if (string.IsNullOrEmpty(Phone))                 return false;             var r = new Regex(@"^\(?([0-9]{3})\)?[-.●]?([0-9]{3})[-.●]?([0-9]{4})$");             return r.IsMatch(Phone);          }         catch (Exception)         {             throw;         }     } 
like image 37
Karan Singh Avatar answered Oct 15 '22 12:10

Karan Singh