regex number range 1-17

杀马特。学长 韩版系。学妹 提交于 2019-11-27 08:07:50

问题


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.

Any ideas? I don't want to have leading zeros.

edited:

The problem is I'm trying to validate a UK postcode like to start with:

KW1-17 (it could be KW1, KW2 up to KW17) ... and this is just the outward part. The postcode may be KW15 1BM or KW151BM so I can't just use anchors to match the whole string ... it becomes more complicated.


回答1:


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:



来源:https://stackoverflow.com/questions/11467548/regex-number-range-1-17

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!