ECM
ECM
总版主
总版主
  • 社区居民
  • 忠实会员
  • 原创写手
阅读:669回复:0

Regular expression for phone numbers

楼主#
更多 发布于:2016-05-30 21:31
I'm trying: \d{3}|\d{11}|\d{11}-\d{1}
 to match three-digit numbers, eleven-digit numbers, eleven-digit followed by a hyphen, followed by one digit. But, it only matches three digit numbers! I also tried \d{3}|\d{11}|\d{11}-\d{1} but doesn't work. Any ideas?
answer(解决方案):

If the first part of an alternation matches, then the regex engine doesn't even try the second part. Presuming you want to match only three-digit, 11 digit, or 11 digit hyphen 1 digit numbers, then you can use lookarounds to ensure that the preceding and following characters aren't digits. (?<!\d)(\d{3}|\d{11}|\d{11}-\d{1})(?!\d)

 

 

 

 

 

 

 

异常中心网是一家专门收集整理程序员编程过程中遇到的常见异常(exception)以及各种异常问答中心的网站。异常中心网旨在,减少程序员在编码遇到异常,处理各种异常时间和痛苦,让程序员能更愉快的、快速的定位异常并查找对应的异常解决方案。异常中心网诚心打造最完美的编程社区为程序员用户服务,努力成为最好的程序员乐园程序员社区程序异常中心程序bug中心异常问答中心

 

喜欢0 评分0
游客

返回顶部