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

前端 未结 7 483
自闭症患者
自闭症患者 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:07

    I have never seen the {m}{n} syntax anywhere. It seems that the regex engine on this Rubular page applies the {2} quantifier to the smallest possible token before that - which is \\d{1}. To mimick this in Java (or most other regex engines, it would seem), you need to group the \\d{1} like so:

    ^(\\d{1}){2}$
    

    See it in action here.

提交回复
热议问题