SWilk
SWilk

Reputation: 3446

What does this preg_match syntax match ".+?"

I know perl style regular expresions fairly well, but today I found one that I do not understand:

preg_match('/^On.+?wrote:.+?$/i',$line); //reduced example

What does the .+? mean? I undarstand the .+ alone, I understand .? alone. But .+?? It seems a bug to me.

The line should match popular citation prefixes in the email body and it is much more complicated along with look behinds, but this is the only part i can't understand, and still the regexp seems to work correclty.

Upvotes: 2

Views: 199

Answers (3)

pawel7318
pawel7318

Reputation: 3583

Lazy not Greedy

.+? matches any character (except newline)

Quantifier: Between one and unlimited times, as few times as possible, expanding as needed [lazy]

check at regex101.com

Upvotes: 0

Sabuj Hassan
Sabuj Hassan

Reputation: 39355

In short, when you add ? its matching least amount possible, where as without ? its matching most amount possible:

Here is the explanation:

NODE                     EXPLANATION
--------------------------------------------------------------------------------
  .+?                      any character except \n (1 or more times
                           (matching the least amount possible))
--------------------------------------------------------------------------------
  .+                       any character except \n (1 or more times
                           (matching the most amount possible))

Upvotes: 2

Realitätsverlust
Realitätsverlust

Reputation: 3953

+ means one or more and is greedy. +? means the same, it just is not greedy like usual regex are.

Edit: I wanted to explain it a little further, but the comment of deceze already explains enough.^^

Upvotes: 2

Related Questions