2

For regular expression match ^(?s).*?HOLIDAY.*?INN.*?EXPRESS.*?$ and ^(?s).*HOLIDAY.*INN.*EXPRESS.*$ what is the benefit of using .*? instead of .* ?

In other words what is the difference of regex quantifier: .* and .*?

Tushar
  • 481
  • 6
  • 26
  • 1
    Dot-star-question-mark (.*?) tells the regex engine: "Match any character, zero or more times, as few times as possible". The engine will start out by matching zero characters, then, because it cannot return a match (since "WORD 2" has not been found), it will match one more character, then one more, and so on. dot means anything can go here and the star means at least 0 times so `.* `accepts any sequence of characters, including an empty string. – khakishoiab Sep 05 '16 at 09:57

1 Answers1

4

* is a greedy quantifier, which means that it will match as many letters possible. *? is a non greedy quantifier, it will match the smallest number of letters possible.

To understand the difference in this case, let's look at the following fragment or the regular expression: HOLIDAY.*?INN. In this one, .*? will match with the text between the string HOLIDAY and the first INN that it will encounter after that. Without the ? it is possible that it would find an INN that is much further far away.

redneb
  • 21,794
  • 6
  • 42
  • 54