What are ^.* and .*$ in regular expressions?

后端 未结 7 1208
臣服心动
臣服心动 2021-02-03 23:05

Can someone explain the meaning of these characters. I\'ve looked them up but I don\'t seem to get it.

The whole regular expression is:

/^.*(?=.{8,})(?=.         


        
7条回答
  •  暗喜
    暗喜 (楼主)
    2021-02-03 23:47

    Main docs: http://www.php.net/manual/en/reference.pcre.pattern.syntax.php

    /^.*(?=.{8,})(?=.*[a-z])(?=.*[A-Z])(?=.*[@#$%^&+=]).*$/
    12345   6         7                                  89
    
    1 - start of pattern, can be almost any character, and must have a matching character at the END of the pattern (see #9 below)
    2 - anchors the pattern to the start of a line of text
    3 - `.` matches any character
    4 - a modifier, "0 or more of whatever came before"
      - `.*` means "0 or more of ANY characters"
    5 - A positive lookahead assertion: http://www.php.net/manual/en/regexp.reference.assertions.php
    6 - A repetition indictor: http://www.php.net/manual/en/regexp.reference.repetition.php
      - `{8,}` = "at least 8 of whatever came previously"
      - `.{8,}` = "at least 8 'any' characters"
    7 - A character class: http://www.php.net/manual/en/regexp.reference.character-classes.php
      - `[a-z]` - any one character in the range 'a' - 'z' (the lower case alphabet)
    8 - anchors the pattern to the end of the line
    9 - end of the pattern, must match character used in #1 above.
    

提交回复
热议问题