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 想问一下树莓派接上显示屏后出现如图所示画面,是什么问题导致的
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line
  • ¥500 火焰左右视图、视差(基于双目相机)
  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号