duanduoding2238 2015-10-26 01:08
浏览 410
已采纳

带有区号的电话#国家代码的正则表达式

I need a regex to match this country-code + area code phone # format:

1-201

where the first two characters are always 1- and the last 3 characters are digits between 201 and 989.

I have ([1][\-][0-9]{3}) currently to specify the 1-xyz and limit length but how can I have the last group to restrict those ranges?

This will be used in PHP.

  • 写回答

4条回答 默认 最新

  • douang4294 2015-10-26 01:14
    关注

    Use this regex:

    ^1\-(2\d[1-9])|([3-8]\d{2})|(9[0-8]\d)$
    

    Here is an explanation of the three capturing groups/ranges:

    (2\d[1-9]) matches 201 to 299
    ([3-8]\d{2}) matches 300 to 899
    (9[0-8]\d) matches 900 to 989

    Here is a link where you can test this regex:

    Regex101

    Update:

    Apparently Laravel doesn't like having so many nested capture groups, but this simplification should work for your needs:

    1-(2\d[1-9]|[3-8]\d{2}|9[0-8]\d)
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

悬赏问题

  • ¥15 乌班图ip地址配置及远程SSH
  • ¥15 怎么让点阵屏显示静态爱心,用keiluVision5写出让点阵屏显示静态爱心的代码,越快越好
  • ¥15 PSPICE制作一个加法器
  • ¥15 javaweb项目无法正常跳转
  • ¥15 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
  • ¥15 java 的protected权限 ,问题在注释里
  • ¥15 这个是哪里有问题啊?