2

我需要一个接受正则表达式格式进行验证的验证框架的正则表达式。我不能使用算术和比较运算符。我想出了一个解决方案,但它没有按预期工作。我想知道我想出的正则表达式有什么问题以及如何正确解决

任何数字的正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]

但这一个不起作用。

4

6 回答 6

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})$

要生成模式编号范围,请访问此处


希望这可以帮助。

于 2012-05-28T04:59:50.370 回答
4
  1. 1[1-9][0-9][0-9][0-9][0-9]- 数字太多。
  2. ^1|2|3实际上意味着(?:^1)|2|3- 你需要^(?:10429|...|40[0-9][0-9][0-9])$
于 2012-05-28T04:59:43.103 回答
0

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}应该做的伎俩。

虽然你为什么需要这个超出了我的范围......

于 2012-05-28T04:59:45.963 回答
0

试试这个^(10429|104[3-9]\d|10[5-9]\d{2}|1[1-9]\d{3}|[2-3]\d{4}|40\d{3})$

于 2012-05-28T05:01:43.840 回答
0

我在这里看到一些很长的正则表达式。这项任务可以在您的模式中没有大量冗余的情况下解决。如果您除了正则表达式之外没有其他任何东西可以使用,那么这就是您想要的模式:

^(([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

于 2012-05-29T13:20:00.860 回答
0

试试这个 - \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

于 2015-03-12T12:56:10.227 回答