regular expressions: match x times OR y times

后端 未结 4 1455
面向向阳花
面向向阳花 2020-12-01 17:46

Lets say I need to match a pattern if it appears 3 or 6 times in a row. The closest I can get is something like \\d{3,6} but that doesn\'t quite do what I need.

\'12

4条回答
  •  离开以前
    2020-12-01 18:45

    ^(\d{3}|\d{6})$
    

    You have to have some sort of terminator otherwise \d{3} will match 1234. That's why I put ^ and $ above. One alternative is to use lookarounds:

    (?

    to make sure it's not preceded by or followed by a digit (in this case). More in Lookahead and Lookbehind Zero-Width Assertions.

提交回复
热议问题