`regex{n,}?` == `regex{n}`?

后端 未结 7 1849
灰色年华
灰色年华 2021-01-20 15:06

-edit- NOTE the ? at the end of .{2,}?

I found out you can write

.{2,}?

Isnt that exactly the same as bel

相关标签:
7条回答
  • 2021-01-20 15:43

    No. {2,} means two times or more while {2} means exactly two times. Quantifiers are greedy by default, so given the string foo you would get foo if you use .{2,}, but fo if you use .{2,}? because you made it lazy. However, the latter is allowed to match more than two times if necessary, but .{2} always means exactly two characters.

    So if you have the string test123 and the pattern .{2,}?\d, you would get test1 because it has to match up to four characters so the \d can also match.

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