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

时间:2021-02-15 22:06:02

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.

有没有办法将preg_match与总字符串长度合并?我需要能够匹配字母数字,字符串中的单个下划线,总字符串长度<= n。

Currently what I'm working with is this:

目前我正在使用的是:

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

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.

我已经玩了很长时间,试图将整个事情整合到preg_match中,所以只是加上了&& strlen,但我确信有更好的方法来做到这一点。

1 个解决方案

#1


1  

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 \n) (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 \n, and the end of
                             the string
----------------------------------------------------------------------
  )                        end of look-ahead
----------------------------------------------------------------------
  .{5,25}                  any character except \n (between 5 and 25
                           times (matching the most amount possible))
----------------------------------------------------------------------
  $                        before an optional \n, and the end of the
                           string
----------------------------------------------------------------------
)                        end of grouping
----------------------------------------------------------------------

Infos on look around

环顾四周的信息

#1


1  

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 \n) (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 \n, and the end of
                             the string
----------------------------------------------------------------------
  )                        end of look-ahead
----------------------------------------------------------------------
  .{5,25}                  any character except \n (between 5 and 25
                           times (matching the most amount possible))
----------------------------------------------------------------------
  $                        before an optional \n, and the end of the
                           string
----------------------------------------------------------------------
)                        end of grouping
----------------------------------------------------------------------

Infos on look around

环顾四周的信息