首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何使用regex格式化电话号码?

如何使用regex格式化电话号码?
EN

Stack Overflow用户
提问于 2015-07-09 17:19:27
回答 3查看 1.2K关注 0票数 0

我需要用一种特定的方式格式化我的电话号码。不幸的是,商业规则禁止我提前做这件事。(单独的输入框等)

格式需要是+1-xxx-xxx-xxxx,其中"+1“是常量。(我们在国际上不做生意)

下面是测试输入的regex模式:

代码语言:javascript
复制
"\\D*([2-9]\\d{2})(\\D*)([2-9]\\d{2})(\\D*)(\\d{4})\\D*"

(这是我从其他地方偷来的)

然后执行regex.Replace(),如下所示:

代码语言:javascript
复制
regex.Replace(telephoneNumber, "+1-$1-$3-$5"); **THIS IS WHERE IT BLOWS UP**

如果我的电话号码在字符串中已经有"+1“,它会加上另一个,这样我就可以得到+1-+1-xxx-xxx-xxxx。

有人能帮忙吗?

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2015-07-09 17:33:20

您可以添加(?:\+1\D*)?以在数字之前捕获一个可选前缀。当它被抓住时,如果它在那里,它就会被替换。

您不需要在号码前后使用\D*。因为它们是可选的,所以它们不会改变任何东西。

您不需要捕获您不使用的部件,这样就可以更容易地看到替换后的结果。

代码语言:javascript
复制
str = Regex.Replace(str, @"(?:\+1\D*)?([2-9]\d{2})\D*([2-9]\d{2})\D*(\d{4})", "+1-$1-$2-$3");

不过,您可以考虑在分隔符中使用比\D*更具体的内容,例如[\- /]?。使用过于非特定的模式,您可能会发现一些不是电话号码的东西,例如将"I have 234 cats, 528 dogs and 4509 horses."转换为"I have +1-234-528-4509 horses."

代码语言:javascript
复制
str = Regex.Replace(str, @"(?:\+1[\- /]?)?([2-9]\d{2})[\- /]?([2-9]\d{2})[\- /]?(\d{4})", "+1-$1-$2-$3");
票数 2
EN

Stack Overflow用户

发布于 2015-07-09 17:51:16

尝试像这样的东西来使事物更易读:

代码语言:javascript
复制
Regex rxPhoneNumber = new Regex( @"
  ^                             # anchor the start-of-match to start-of-text
  \D*                           # - allow and ignore any leading non-digits
  1?                            # - we'll allow (and ignore) a leading 1 (as in 1-800-123-4567
  \D*                           # - allow and ignore any non-digits following that
  (?<areaCode>[2-9]\d\d)        # - required 3-digit area code
  \D*                           # - allow and ignore any non-digits following the area code
  (?<exchangeCode>[2-9]\d\d)    # - required 3-digit exchange code (central office)
  \D*                           # - allow and ignore any non-digits following the C.O.
  (?<subscriberNumber>\d\d\d\d) # - required 4-digit subscriber number
  \D*                           # - allow and ignore any non-digits following the subscriber number
  $                             # - followed the end-of-text.
  " ,
  RegexOptions.IgnorePatternWhitespace|RegexOptions.ExplicitCapture
);

string input = "voice: 1 (234) 567/1234 (leave a message)" ;
bool isValid = rxPhoneNumber.IsMatch(input) ;
string tidied = rxPhoneNumber.Replace( input , "+1-${areaCode}-${exchangeCode}-${subscriberNumber}" ) ;

,这将为tidied提供所需的值。

代码语言:javascript
复制
+1-234-567-1234
票数 1
EN

Stack Overflow用户

发布于 2015-07-09 17:27:19

您可以使用以下正则表达式

代码语言:javascript
复制
\D*(\+1-)?([2-9]\d{2})\D*([2-9]\d{2})\D*(\d{4})\D*

和替换字符串:

代码语言:javascript
复制
$1$2-$3-$4

这是一个演示

这是对你所拥有的正则表达式的调整。如果你需要匹配所有的数字,我会用

代码语言:javascript
复制
(\+1-)?\b([2-9]\d{2})\D*([2-9]\d{2})\D*(\d{4})\b

请参阅演示2

另外,如果\+1-中的连字符是可选的,则添加一个?\+1-?

为了使正则表达式更安全,我将\D* (0或更多的非数字符号)替换为包含已知分隔符的字符类,例如[ /-]* (匹配/、空格和-s)。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/31324230

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档