正则表达式匹配所有我们的电话号码格式

con*_*ind 15 c# regex visual-studio

首先,我会说我在这里看到很多例子和谷歌搜索,但没有发现匹配所有条件我正在寻找一些匹配前三名不低于一些中间.请告诉我如何将所有这些放在一个地方.

(xxx)xxxxxxx
(xxx) xxxxxxx
(xxx)xxx-xxxx
(xxx) xxx-xxxx
xxxxxxxxxx
xxx-xxx-xxxxx
Run Code Online (Sandbox Code Playgroud)

用作:

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

             }
Run Code Online (Sandbox Code Playgroud)

样本输出:

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
Run Code Online (Sandbox Code Playgroud)

Fly*_*del 43

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

  • 没问题,最近似乎变得越来越自命不凡了.不能说我是粉丝. (7认同)
  • 具有讽刺意味的是,在几个如此答案中,这个最能满足我的需求,所以感谢您的回答。仅供参考,我也对匹配点和空格感兴趣,所以`\(?\d{3}\)?[。-]?*\d{3}[. -]?*[。-]?\d{4}` 帮我解决了这个问题。 (3认同)

Kar*_*ngh 9

 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;
        }
    }
Run Code Online (Sandbox Code Playgroud)


Jam*_*zba 5

为了扩展FlyingStreudel的正确答案,我对其进行了修改以接受“。”。作为分隔符,这是我的要求。

\(?\d{3}\)?[-\.]? *\d{3}[-\.]? *[-\.]?\d{4}

使用中(在字符串中查找所有电话号码):

string text = "...the text to search...";
string pattern = @"\(?\d{3}\)?[-\.]? *\d{3}[-\.]? *[-\.]?\d{4}";
Regex regex = new Regex(pattern, RegexOptions.IgnoreCase);
Match match = regex.Match(text);
while (match.Success)
{
    string phoneNumber = match.Groups[0].Value;
    //TODO do something with the phone number
    match = match.NextMatch();
}
Run Code Online (Sandbox Code Playgroud)


小智 5

为了添加上述所有建议,这是我的正则表达式,它将强制执行 NANP 标准:

((?:\(?[2-9](?:(?=1)1[02-9]|(?:(?=0)0[1-9]|\d{2}))\)?\D{0,3})(?:\(?[2-9](?:(?=1)1[02-9]|\d{2})\)?\D{0,3})\d{4})
Run Code Online (Sandbox Code Playgroud)

此正则表达式强制执行 NANP 标准规则,例如N11 codes are used to provide three-digit dialing access to special services,因此使用条件捕获排除它们。它还在各部分之间最多占 3 个非数字字符 ( \D{0,3}),因为我看到了一些奇怪的数据。

根据提供的测试数据,输出如下:

3087774825
(281)388-0388
(281)388-0300
(979) 778-0978
(281)934-2479
(281)934-2447
(979)826-3273
(979)826-3255
(281)356-2530
(281)356-5264
(936)825-2081
(832)595-9500
(832)595-9501
281-342-2452
Run Code Online (Sandbox Code Playgroud)

请注意,由于 NANP 标准中的电话号码不是有效电话号码,因此省略了两个示例值: 区号以 1 开头

1334714149
1334431660
Run Code Online (Sandbox Code Playgroud)

我所指的规则可以在国家 NANPA 网站的区号页面上找到,其中指出:The format of an area code is NXX, where N is any digit 2 through 9 and X is any digit 0 through 9.