What is double plus in regular expressions?

后端 未结 2 1251
名媛妹妹
名媛妹妹 2020-12-14 15:21

I have been seeing this in some PHP script:

[a-zA-Z0-9_]++

What does the double plus mean?

2条回答
  •  自闭症患者
    2020-12-14 16:01

    To give you a very simple example:

    Let's say you have a string "123". The matched characters have a ^ underneath in the following examples.

    1. Regex: \d+?. partial match!

      123  # The \d+? eats only 1 because he's lazy (on a diet) and leaves the 2 to the .(dot).
      ^^   # This means \d+? eats as little as possible.
      
    2. Regex: \d+. full match!

      123  # The \d+ eats 12 and leaves the 3 to the .(dot).
      ^^^  # This means \d+ is greedy but can still share some of his potential food to his neighbour friends.
      
    3. Regex: \d++. no match!

      123  # The \d++ eats 123. He would even eat more if there were more numbers following. 
           # This means \d++ is possessive. There is nothing left over for the .(dot), so the pattern can't be matched.
      

提交回复
热议问题