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

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

    When I input your regex in RegexBuddy using the Java regex syntax, it displays following message

    Quantifiers must be preceded by a token that can be repeated «{2}»

    Changing the regex to explicitly use a grouping ^(\d{1}){2} solves that error and works as you expect.


    I assume that the java regex engine simply neglects the error/expression and works with what has been compiled so far.

    Edit

    The reference to the IEEE-Standard in @piet.t's answer seems to support that assumption.

    Edit 2 (kudos to @fncomp)

    For completeness, one would typically use (?:)to avoid capturing the group. The complete regex then becomes ^(?:\d{1}){2}

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