uzay95
uzay95

Reputation: 16632

Why greedy plus behaves different than star?

With * and + greedies behaves different in the below regexp patterns, why?

This is my text:

hello abcdef ghijklmc happiness<span>Lorem impsum</span> lorem <p>Lorem impsum</p>Lorem impsum Today is Feb 23rd, 2003

This is regexp:

<[/]?[a-z].*?>

Result:

enter image description here

With this pattern:

<[/]?[a-z].+?>

Result:

enter image description here

Upvotes: 0

Views: 42

Answers (1)

Quentin
Quentin

Reputation: 943746

Because * is 0 or more and + is 1 or more.

When the tag name only has one character in it:

  1. [a-z] matches the p
  2. . matches the >
    • If you have a + the > has to be matched by the . to it keeps matching until the next > (at the end of the next tag)
    • If you have a *, the > doesn't have to be matched by the . (since you can have 0 matches) so the > matches that character instead.
  3. The > matches the next >

Upvotes: 5

Related Questions