duanlan5320 2013-09-13 03:27
浏览 241
已采纳

正则表达式匹配从数字2开始的8个字母数字,并且至少有两个数字

Currently I have the following regular expression that matches 8 digits alphanumeric, I want to modify it such that it has to start with the number 2 and contains at least 2 numbers in this 8 digit. How can I do so?

preg_match('/[A-Za-z0-9]{8}/', $bio)
  • 写回答

2条回答 默认 最新

  • duanjuhuo8772 2013-09-13 07:54
    关注

    How about:

    /^(?=2.*\d)[a-zA-Z0-9]{8}$/
    

    if the number 2 counts for one of the 2 required number.

    /^(?=2.*\d.*\d)[a-zA-Z0-9]{8}$/
    

    if the number 2 doesn't count for one of the 2 required number.

    explanation:

    The regular expression:
    
    (?-imsx:^(?=2.*\d)[a-zA-Z0-9]{8}$)
    
    matches as follows:
    
    NODE                     EXPLANATION
    ----------------------------------------------------------------------
    (?-imsx:                 group, but do not capture (case-sensitive)
                             (with ^ and $ matching normally) (with . not
                             matching 
    ) (matching whitespace and #
                             normally):
    ----------------------------------------------------------------------
      ^                        the beginning of the string
    ----------------------------------------------------------------------
      (?=                      look ahead to see if there is:
    ----------------------------------------------------------------------
        2                        '2'
    ----------------------------------------------------------------------
        .*                       any character except 
     (0 or more times
                                 (matching the most amount possible))
    ----------------------------------------------------------------------
        \d                       digits (0-9)
    ----------------------------------------------------------------------
      )                        end of look-ahead
    ----------------------------------------------------------------------
      [a-zA-Z0-9]{8}           any character of: 'a' to 'z', 'A' to 'Z',
                               '0' to '9' (8 times)
    ----------------------------------------------------------------------
      $                        before an optional 
    , and the end of the
                               string
    ----------------------------------------------------------------------
    )                        end of grouping
    ----------------------------------------------------------------------
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 如何获取烟草零售终端数据
  • ¥15 数学建模招标中位数问题
  • ¥15 phython路径名过长报错 不知道什么问题
  • ¥15 深度学习中模型转换该怎么实现
  • ¥15 HLs设计手写数字识别程序编译通不过
  • ¥15 Stata外部命令安装问题求帮助!
  • ¥15 从键盘随机输入A-H中的一串字符串,用七段数码管方法进行绘制。提交代码及运行截图。
  • ¥15 TYPCE母转母,插入认方向
  • ¥15 如何用python向钉钉机器人发送可以放大的图片?
  • ¥15 matlab(相关搜索:紧聚焦)