Difference between “.+” and “.+?”

Both will match any sequence of one or more characters. The difference is that:

  • .+ is greedy and consumes as many characters as it can.
  • .+? is reluctant and consumes as few characters as it can.

See Differences Among Greedy, Reluctant, and Possessive Quantifiers in the Java tutorial.

Thus:

  • e.+d finds the longest substring that starts with e and ends with d (and contains at least one character in between). In your example extend cup end will be found.
  • e.+?d find the shortest such substring. In your example, extend and end are two such non-overlapping matches, so it finds both.

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)