How does {m}{n} (“exactly n times” twice) work?

前端 未结 7 502
自闭症患者
自闭症患者 2021-02-03 16:23

So, some way or another (playing around), I found myself with a regex like \\d{1}{2}.

Logically, to me, it should mean:

(A digit exac

7条回答
  •  星月不相逢
    2021-02-03 17:03

    Scientific approach:
    click on the patterns to see the example on regexplanet.com, and click on the green Java button.

    • You've already showed \d{1}{2} matches "1", and doesn't match "12", so we know it isn't interpreted as (?:\d{1}){2}.
    • Still, 1 is a boring number, and {1} might be optimized away, lets try something more interesting:
      \d{2}{3}. This still only matches two characters (not six), {3} is ignored.
    • Ok. There's an easy way to see what a regex engine does. Does it capture?
      Lets try (\d{1})({2}). Oddly, this works. The second group, $2, captures the empty string.
    • So why do we need the first group? How about ({1})? Still works.
    • And just {1}? No problem there.
      It looks like Java is being a little weird here.
    • Great! So {1} is valid. We know Java expands * and + to {0,0x7FFFFFFF} and {1,0x7FFFFFFF}, so will * or + work? No:

      Dangling meta character '+' near index 0
      +
      ^

      The validation must come before * and + are expanded.

    I didn't find anything in the spec that explains that, it looks like a quantifier must come at least after a character, brackets, or parentheses.

    Most of these patterns are considered invalid by other regex flavors, and for a good reason - they do not make sense.

提交回复
热议问题