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

后端 未结 7 1850
灰色年华
灰色年华 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:37

    No, they are different. ^.{2,}?$ matches strings whose length is at least 2 (as seen on rubular.com):

    12
    123
    1234
    

    By contrast, ^.{2}$ only matches strings whose length is exactly 2 (as seen on rubular.com).

    It's correct that being reluctant, .{2,}? will first attempt to match only two characters. But for the overall pattern to match, it can take more. This is not the case with .{2}, which can only match exactly 2 characters.

    References

    • regular-expressions.info/Repetition

    Related questions

    • Difference between .*? and .* for regex

提交回复
热议问题