正则表达式匹配所有美国电话号码格式

25

首先,我想说我在这里看过很多例子并谷歌搜索过,但没有一个符合我所寻找的所有条件,我正在寻找前三个匹配项而不是下面的一些或中间的一些。

请告诉我如何将它们放在一个地方。

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

使用方式:

  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(); ;

             }

输出示例:

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
11个回答

0
((\+\d[- .]?)?\(?\d{3}\)?[- .]?/?\d{3}[- ./]?\d{4}([ ]?(x|ext|extension)[.]?[ ]?\d{4})?)|(^\(\d{3}\) \d{3}[./-]\d{4} x\d{1,4}$)|(?:(?:\+?1[-. ])?(?:\(\d{3}\)|\d{3})[-. ]?\d{3}[-. ]?\d{4}|\d{10})\b|\b\d{3}-\d{3}-\d{4}\b|\b\d{3}\.\d{3}\.\d{4}\b|\b\d{3}\/\d{3}-\d{4}\b|\b\(\d{3}\) \d{3}\.\d{4}\b|\b\(\d{3}\) \d{3}\/\d{4}\b|\b\(\d{3}\) \d{3} \d{4}\b|\b1[-. ]?\d{3}[-. ]?\d{3}[-. ]?\d{4}

这将匹配

917.974.4266
(123) 456-7890
123-456-7890
123.456.7890
123/456-7890
1234567890
+1 (123) 456-7890
+1 123-456-7890
+1 123.456.7890
+1 123/456-7890
+11234567890
(123) 456.7890
(123) 456/7890
(123) 456 7890
1-123-456-7890
1.123.456.7890
1/123/456/7890
1 123 456 7890
1 123.456.7890
1 123/456-7890
123 4567890
123/4567890
123-4567890
123.4567890
(123) 456.7890 ext. 1234
(123) 456-7890 ext. 1234
123-456-7890 ext. 1234
123.456.7890 ext. 1234
123/456-7890 ext. 1234
1234567890 ext. 1234
+1 (123) 456-7890 ext. 1234
+1-123-456-7890
+1 123-456-7890
+1.123.456.7890
+1/123/456/7890
+11234567890
123-456-7890
123.456.7890
123/456-7890
1234567890
(123) 456-7890
(123)456-7890
(123)4567890
(123) 456.7890
(123) 456/7890
(123) 456 7890
1-123-456-7890
1.123.456.7890
1/123/456/7890
1 123 456 7890
1 123.456.7890
1 123/456-7890
123 4567890
123/4567890
123-4567890
123.4567890
(123) 456-7890 ext. 1234
(123) 456.7890 ext. 1234
(123) 456/7890 ext. 1234
123-456-7890 ext. 1234
123.456.7890 ext. 1234
123/456-7890 ext. 1234
1234567890 ext. 1234
+1-123-456-7890 ext. 1234
+1 123-456-7890 ext. 1234
+1.123.456.7890 ext. 1234
+1/123/456/7890 ext. 1234
+11234567890 ext. 1234
(123) 456-7890 x1234
(123) 456.7890 x1234
(123) 456/7890 x1234
123-456-7890 x1234
123.456.7890 x1234
123/456-7890 x1234
1234567890 x1234
+1-123-456-7890 x1234
+1 123-456-7890 x1234
+1.123.456.7890 x1234
+1/123/456/7890 x1234
+11234567890 x1234
123-456-7890 x1234

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接