regex number range 1-17

后端 未结 1 1851
Happy的楠姐
Happy的楠姐 2020-12-11 20:15

Can someone tell me how to match a number between 1-17 with a regex:

I tried [1-9]|1[0-7] but it matches 8 in the 18 for example because of the first part.

A

1条回答
  •  無奈伤痛
    2020-12-11 21:01

    You need to put anchors around the regex or it will match substrings:

    ^(?:[2-9]|1[0-7]?)$
    

    I've also taken the liberty to make your regex a bit more efficient:

    Comparison of two regexes

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