I am trying to create a regex for a exactly five digit number which should be in the range between 90000 – 96163. I created a regex for exactly 5 number @"^\d{5}$" Now how do I make sure that it is between the range of 90000 – 96163? Anything smaller than 90001 and over 96162 should not work.
我正在尝试为正好五位数创建一个正则表达式,该数字应该在90000 - 96163之间。我创建了一个正则数为5的数字@“^ \ d {5} $”现在我如何确保它介于90000 - 96163之间?任何小于90001和超过96162的东西都不应该起作用。
Thanks
谢谢
3 个解决方案
#1
2
This is most easily achieved using a regular numeric comparison (using <
and >
operators) in your language.
使用您的语言使用常规数字比较(使用 <和> 运算符)最容易实现。
You can do a range check using regular expressions, but it's tedious to implement and all but nicely readable. For the sake of completeness, here's a possible pattern:
您可以使用正则表达式进行范围检查,但实现起来很繁琐,而且可读性很好。为了完整起见,这是一个可能的模式:
9([0-5][0-9]{3}|6(0[0-9]{2}|1([0-5][0-9]|6[0-3])))
Broken up, the pattern reads as follows:
分手了,模式如下:
9 # The first digit must be a 9
(
[0-5][0-9]{3} # Covering the range 90000-95999
|
6 # Matching 96xxx
(
0[0-9]{2} # Covering the range 96000-96099
|
1 # Matching 961xx
(
[0-5][0-9] # Covering the range 96100-96159
|
6[0-3] # Covering the range 96160-96163
)
)
)
Please don't do this if it can be avoided. Just consider what happens when the range boundaries change: Imagine you have to check whether a value is between 7243 and 132843 — not fun.
如果可以避免,请不要这样做。只要考虑范围边界变化时会发生什么:想象一下,你必须检查一个值是否在7243和132843之间 - 不好玩。
#2
0
Digit by digit:
数字:
/(?!^90000$)(^9([0-5]\d{3}|6(0\d{2}|1([0-5]\d|6[0-2]))))$/
#3
0
(9[0-5][0-9]{3}|960[0-9]{2}|961[0-5][0-9]|9616[0-3])
http://gamon.webfactional.com/regexnumericrangegenerator/
http://gamon.webfactional.com/regexnumericrangegenerator/
This would be enough to find a regex to find a range. No need to ask about range problems
这足以找到一个正则表达式来查找范围。无需询问范围问题
#1
2
This is most easily achieved using a regular numeric comparison (using <
and >
operators) in your language.
使用您的语言使用常规数字比较(使用 <和> 运算符)最容易实现。
You can do a range check using regular expressions, but it's tedious to implement and all but nicely readable. For the sake of completeness, here's a possible pattern:
您可以使用正则表达式进行范围检查,但实现起来很繁琐,而且可读性很好。为了完整起见,这是一个可能的模式:
9([0-5][0-9]{3}|6(0[0-9]{2}|1([0-5][0-9]|6[0-3])))
Broken up, the pattern reads as follows:
分手了,模式如下:
9 # The first digit must be a 9
(
[0-5][0-9]{3} # Covering the range 90000-95999
|
6 # Matching 96xxx
(
0[0-9]{2} # Covering the range 96000-96099
|
1 # Matching 961xx
(
[0-5][0-9] # Covering the range 96100-96159
|
6[0-3] # Covering the range 96160-96163
)
)
)
Please don't do this if it can be avoided. Just consider what happens when the range boundaries change: Imagine you have to check whether a value is between 7243 and 132843 — not fun.
如果可以避免,请不要这样做。只要考虑范围边界变化时会发生什么:想象一下,你必须检查一个值是否在7243和132843之间 - 不好玩。
#2
0
Digit by digit:
数字:
/(?!^90000$)(^9([0-5]\d{3}|6(0\d{2}|1([0-5]\d|6[0-2]))))$/
#3
0
(9[0-5][0-9]{3}|960[0-9]{2}|961[0-5][0-9]|9616[0-3])
http://gamon.webfactional.com/regexnumericrangegenerator/
http://gamon.webfactional.com/regexnumericrangegenerator/
This would be enough to find a regex to find a range. No need to ask about range problems
这足以找到一个正则表达式来查找范围。无需询问范围问题