21

首先,我会说我在这里看到了很多例子并用谷歌搜索,但没有发现符合我正在寻找的匹配前 3 名而不低于中间值的所有条件。请让我知道如何将它们全部放在一个地方。

(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
4

8 回答 8

58

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

于 2013-08-06T22:07:59.327 回答
11
 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;
        }
    }
于 2015-06-09T11:45:35.050 回答
6

为了扩展 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();
}
于 2017-01-23T19:44:48.720 回答
4

帮助自己。不要为此使用正则表达式。Google 发布了一个很棒的库来处理这个特定的用例:libphonenumber。有一个lib 的在线演示

public static void Main()
{
    var phoneUtil = PhoneNumberUtil.GetInstance();
    var numberProto = phoneUtil.Parse("(979) 778-0978", "US");
    var formattedPhone = phoneUtil.Format(numberProto, PhoneNumberFormat.INTERNATIONAL);
    Console.WriteLine(formattedPhone);
}

.NETFiddle 上的演示

于 2018-07-10T09:53:15.740 回答
3

要添加上述所有建议,这是我的 RegEx,它将强制执行 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})

此正则表达式强制执行 NANP 标准规则,例如N11 codes are used to provide three-digit dialing access to special services,因此使用条件捕获将它们排除在外。它还占\D{0,3}部分之间最多 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

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

1334714149
1334431660

我所指的规则可以在国家 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.

于 2017-11-06T14:25:18.517 回答
1
^?\(?\d{3}?\)??-??\(?\d{3}?\)??-??\(?\d{4}?\)??-?$

这允许:

  • (123)-456-7890
  • 123-456-7890
于 2019-05-31T20:43:00.633 回答
1

对于 c#,美国电话号码验证应如下所示

^\(?\d{3}?\)??-??\(?\d{3}?\)??-??\(?\d{4}?\)??-?$

777-777-7777

于 2020-05-08T10:29:38.300 回答
1

我会以不同的方式处理这种情况,而是:

  • 第 1 步:从给定文本中提取您需要的数字。(如果您有数字列表,请跳过)
  • 第 2 步:循环数字并清理格式
  • 注意:最后一个数字格式错误,但代码仅占用可用部分。

示例文本

Here are a list of contacts you can call:
Justin at +11 (949) 255 6458 or 1-909-885-4469.
Smith at (855) 270 4206 or 555-555-5555.
Sammy at 767.456.5289 or 9876548521x355.
Jill at 254.8695 or 56-852-6645 ext 22

代码

// Step 1: Match numbers from text;
let likePhoneRegex = /([0-9][0-9\s\.\-\(\)\[\]]+[0-9]{4})(\s*?(x|ext|extension)\s*[0-9]{2,})?/gi;
let matchData = sample.match(likePhoneRegex);

// Step 2: Loop and clean up numbers;
let phoneRegex = /(([0-9]*?)([2-9][0-9]{2})?([2-9][02-9]{2})([0-9]{4}))\b/;
let cleanupRegex = /(\+\.\.?)|(\+.*?\.\.)/;
let cleanData = [];
for( var i=0, il=matchData.length, num; i<il; i++){
   num = matchData[i].replace(/[^0-9\x]/g,'')
   var [ phone, ext ] = num.split('x');
   cleanData.push( phone.replace( phoneRegex, "+$2.$3.$4.$5" ).replace( cleanupRegex, '' ) + ( ext && ext.length > 0 ? ' x' + ext : '' ) );
}

结果

[
   "+11.949.255.6458",
   "+1.909.885.4469",
   "855.270.4206",
   "555.555.5555",
   "767.456.5289",
   "987.654.8521 x355",
   "254.8695",
   "852.6645 x22"
]
于 2021-07-26T17:39:07.660 回答