doufan1899 2013-09-11 07:48 采纳率: 100%
浏览 36
已采纳

尝试做preg_match以及匹配整个字符串长度

Is there a way to incorporate preg_match with total string length? I need to be able to match alphanumeric, with single underscores inside the string, with a total string length <= n.

Currently what I'm working with is this:

preg_match('/^[A-Za-z0-9]*(?:_[A-Za-z0-9]+)*$/',$string) && (strlen($string) <= 10)

I have played around with this for too long, trying to incorporate the entire thing into preg_match, so just tacked on the && strlen, but I'm sure there is a better way to do this.

  • 写回答

1条回答 默认 最新

  • dphs48626 2013-09-11 08:35
    关注

    Have a try with:

    preg_match('/^(?=[A-Za-z0-9]*(?:_[A-Za-z0-9]+)*).{1,10}$/', $string)
    

    edit according to comments:

    /^(?=[A-Za-z0-9]+(?:_[A-Za-z0-9]+)*$).{5,25}$/
    

    explanation:

    The regular expression:
    
    (?-imsx:^(?=[A-Za-z0-9]+(?:_[A-Za-z0-9]+)*$).{5,25}$)
    
    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:
    ----------------------------------------------------------------------
        [A-Za-z0-9]+             any character of: 'A' to 'Z', 'a' to
                                 'z', '0' to '9' (1 or more times
                                 (matching the most amount possible))
    ----------------------------------------------------------------------
        (?:                      group, but do not capture (0 or more
                                 times (matching the most amount
                                 possible)):
    ----------------------------------------------------------------------
          _                        '_'
    ----------------------------------------------------------------------
          [A-Za-z0-9]+             any character of: 'A' to 'Z', 'a' to
                                   'z', '0' to '9' (1 or more times
                                   (matching the most amount possible))
    ----------------------------------------------------------------------
        )*                       end of grouping
    ----------------------------------------------------------------------
        $                        before an optional 
    , and the end of
                                 the string
    ----------------------------------------------------------------------
      )                        end of look-ahead
    ----------------------------------------------------------------------
      .{5,25}                  any character except 
     (between 5 and 25
                               times (matching the most amount possible))
    ----------------------------------------------------------------------
      $                        before an optional 
    , and the end of the
                               string
    ----------------------------------------------------------------------
    )                        end of grouping
    ----------------------------------------------------------------------
    

    Infos on look around

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

报告相同问题?

悬赏问题

  • ¥15 wegame打不开英雄联盟
  • ¥15 公司的电脑,win10系统自带远程协助,访问家里个人电脑,提示出现内部错误,各种常规的设置都已经尝试,感觉公司对此功能进行了限制(我们是集团公司)
  • ¥15 救!ENVI5.6深度学习初始化模型报错怎么办?
  • ¥30 eclipse开启服务后,网页无法打开
  • ¥30 雷达辐射源信号参考模型
  • ¥15 html+css+js如何实现这样子的效果?
  • ¥15 STM32单片机自主设计
  • ¥15 如何在node.js中或者java中给wav格式的音频编码成sil格式呢
  • ¥15 不小心不正规的开发公司导致不给我们y码,
  • ¥15 我的代码无法在vc++中运行呀,错误很多