正则表达式匹配我们所有的电话号码格式 [英] Regex to match all us phone number formats

查看:249
本文介绍了正则表达式匹配我们所有的电话号码格式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

首先我要说我在这里看到了很多例子,用Google搜索,但没有发现匹配所有我找不低于某些插图中的一些比赛前3名的条件。
请让我知道如何把它们都在同一个地方。

 (XXX)XXXXXXX 
(XXX)XXXXXXX
(XXX)XXX-XXXX
(XXX)XXX-XXXX
XXXXXXXXXX
XXX-XXX-XXXXX

使用为:

 常量字符串MatchPhonePattern = 
@\ - * \d {3} - * - \d {4}(\d {3} \?)????
正则表达式RX =新的正则表达式(MatchPhonePattern,RegexOptions.Compiled | RegexOptions.IgnoreCase);
//查找匹配。
MatchCollection匹配= rx.Matches(文本);
//报告发现匹配的数量。
INT noOfMatches = matches.Count;
//在每场比赛的报告。

的foreach(赛的比赛在比赛中)
{

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


解决方案

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


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

解决方案

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

这篇关于正则表达式匹配我们所有的电话号码格式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆