In a regex, what changes when you add a question mark to .+

后端 未结 2 1075
耶瑟儿~
耶瑟儿~ 2020-12-12 05:01

I was browsing old php sources and I found a pattern I don\'t understand (probably a copy/past from the internet some times ago ...).

Here is a simple exemple using

相关标签:
2条回答
  • 2020-12-12 05:19

    + is a greedy operator; consuming as much as possible. Therefore, .+ will match as much as it can and still allow the remainder of the regular expression to match. Once you specify the question mark +?, you're telling the regex engine (don't be greedy.. as soon as you find a double quote "... stop, you're done.)

    0 讨论(0)
  • 2020-12-12 05:21

    The ? makes the match non-greedy, meaning that the expression .+? will match as few characters as possible to make the regex match, rather than matching as any as possible, which is the default behaviour.

    0 讨论(0)
提交回复
热议问题