您可以“或”您的正则表达式,但这意味着重复:
^([12]|[12][^1-6]|[12][^1-6]\d{2})$
要打破这一点:
^ From the start of the string / line
match either
[12] The numbers 1 or 2
| or
[12] The numbers 1 or 2
followed by
[^1-6] Any character which is not the numbers 1 through 6
| or
[12] The numbers 1 or 2
followed by
[^1-6] Any character which is not the numbers 1 through 6
followed by
\d Any number
{2} repeated two times
$ followed by the end of the string / line
这将匹配以下任何一项:
1
2
1N
2N
1NXX
2NXX
N
不是数字 1 到 6的任何数字都在哪里X
。
注意:如果您真的只想匹配正则表达式第二部分中的数字 0 和 7-9,只需更改[^1-6]
为[07-9]
.