[英]Solving a Regex for a number range
我需要一个验证框架的regfex,它接受用于验证的正则表达式格式。 我不能使用算术和比较运算符。 我提出了一个解决方案,但它没有按预期工作。 我想知道我想出的正则表达式有什么问题以及如何正确排序
正则表达式从10429
到40999
任何数字
我的解决方案
^1042[9-9]|104[3-9][0-9]|10[5-9][0-9][0-9]|1[1-9][0-9][0-9][0-9][0-9]|[2-3][0-9][0-9][0-9][0-9]|40[0-9][0-9][0-9]
但是这个没有用。
尝试这个
^(10429|104[3-9][0-9]|10[5-9][0-9]{2}|1[1-9][0-9]{3}|[23][0-9]{4}|40[0-9]{3})$
要生成模式编号Ranges,请访问此处 。
希望这可以帮助。
1[1-9][0-9][0-9][0-9][0-9]
- 数字太多。 ^1|2|3
实际上意味着(?:^1)|2|3
- 你需要^(?:10429|...|40[0-9][0-9][0-9])$
。 10429|104[3-9][0-9]|10[5-9][0-9]{2}|1[1-9][0-9]{3}|[23][0-9]{4}|40[0-9]{3}
应该做到这一点。
虽然为什么你需要这个超出我...
试试这个^(10429|104[3-9]\\d|10[5-9]\\d{2}|1[1-9]\\d{3}|[2-3]\\d{4}|40\\d{3})$
我在这看到一些非常长的正则表达式。 无需在模式中进行大量冗余即可解决此任务。 如果你没有正则表达式以外的任何东西,这里是你想要的模式:
^(([123][1-9]|[234]0)[0-9]{3}|10([5-9][0-9]{2}|4([3-9][0-9]|29)))$
在这里它被扩展以显示它的含义:
^ #The beginning of the line. This ensures 10429 passes, but 9999910429 doesn't.
(
([123][1-9]|[234]0) #This lets the first two digits be anything from 11-40. We'll deal with 10xxx later on.
[0-9]{3} #If the first two digits are 11-40, then the last three can be anything from 000-999.
|
10 #Okay, we've covered 11000-40999. Now for 10429-10999.
(
[5-9][0-9]{2} #Now we've covered 10500-10999; on to the 104xx range.
|
4
(
[3-9][0-9] #Covers 10430-10499.
|
29 #Finally, the special case of 10429.
)
)
)
$ #The end of the line. This ensures 10429 passes, but 104299999 doesn't.
如果数字不是整个输入,而是嵌入在字符串中(例如,您想要从字符串“blah blah 11000 foo 39256 bar 22222”中获取所有数字,请将^
和$
替换$
\\b
。
看到它在Regexr上工作: http: //regexr.com?312p0
试试这个 - \\ b(10429 | 104 [3-9] [0-9] | 10 [5-9] [0-9] {2} | 1 [1-9] [0-9] {3} | [23] [0-9] {4} | 40 [0-9] {3})\\ b
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.