35

I have been seeing this in some PHP script:

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

What does the double plus mean?

Mark Amery
  • 110,735
  • 57
  • 354
  • 402
Teej
  • 12,352
  • 9
  • 68
  • 92

2 Answers2

39

That's a Possessive Quantifier.

It basically means that if the regex engine fails matching later, it will not go back and try to undo the matches it made here. In most cases, it allows the engine to fail much faster, and can give you some control where you need it - which is very rare for most uses.

Kobi
  • 125,267
  • 41
  • 244
  • 277
  • 3
    Just to clear this up: it can undo the *whole* group, but nothing *inside the group*. I'm not sure that was clear enough, but the link explain it very well. – Kobi Dec 20 '10 at 13:04
  • 3
    Can you provide example expose difference ++ and + ? – gstackoverflow May 23 '14 at 11:04
29

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.
    
Andre Elrico
  • 8,959
  • 1
  • 37
  • 61