Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is double plus in regular expressions?

Tags:

regex

php

I have been seeing this in some PHP script:

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

What does the double plus mean?

like image 314
Teej Avatar asked Dec 20 '10 12:12

Teej


People also ask

What is plus symbol in regex?

A regular expression followed by a plus sign ( + ) matches one or more occurrences of the one-character regular expression. If there is any choice, the first matching string in a line is used. A regular expression followed by a question mark ( ? ) matches zero or one occurrence of the one-character regular expression.

What is the meaning of \\ in regular expression?

\\ is technically one backslash, but you gotta type two because it's in a string. It's escaping the . . \\' matches the end of a string, but $ can also match the end of a line. The difference might be relevant if you have newlines in a string.

What does 2 mean in regular expression?

2. It thus means that the character in the second group is repeated. So this matches string where a character is repeated two times (or more). – Willem Van Onsem. Oct 2, 2018 at 18:29.

What's the difference between () and [] in regular expression?

[] denotes a character class. () denotes a capturing group. (a-z0-9) -- Explicit capture of a-z0-9 . No ranges.


2 Answers

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.

like image 112
Kobi Avatar answered Oct 09 '22 18:10

Kobi


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.
    
like image 29
Andre Elrico Avatar answered Oct 09 '22 16:10

Andre Elrico