dougu7546 2014-04-03 05:17
浏览 2012
已采纳

正则表达式以特定数字开头

How to write regular expression for following conditions:

  1. Should have only numbers
  2. Must be 8 digits long
  3. Must start with 8 or 9 or 6

So for I can do only for first two conditions. I am not sure how to do the third conditions My code is

 if (!preg_match('/^[0-9]{8}$/', $number))
  • 写回答

1条回答 默认 最新

  • doujuanqi2909 2014-04-03 05:20
    关注

    You're almost there. Simply remove the first number from the character class and validate it at the start of your pattern...

    /^(8|9|6)\d{7}$/
    

    FYI - \d is the escape sequence for digits. I suppose you could also use this

    /^[896]\d{7}$/
    

    as it means about the same thing when you're only watching for a single character at the start.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 delta降尺度方法,未来数据怎么降尺度
  • ¥15 c# 使用NPOI快速将datatable数据导入excel中指定sheet,要求快速高效
  • ¥15 高德地图点聚合中Marker的位置无法实时更新
  • ¥15 DIFY API Endpoint 问题。
  • ¥20 sub地址DHCP问题
  • ¥15 delta降尺度计算的一些细节,有偿
  • ¥15 Arduino红外遥控代码有问题
  • ¥15 数值计算离散正交多项式
  • ¥30 数值计算均差系数编程
  • ¥15 redis-full-check比较 两个集群的数据出错