Regex以匹配特定的数字格式。

时间:2023-01-17 23:36:59

I am having trouble figuring out how to write a regex to match a number (technically a string) with the following rules:

我不知道如何编写一个regex来将数字(技术上是字符串)与以下规则匹配:

  • all numeric
  • 所有的数字
  • must be exactly 11 digits
  • 一定是11位吗
  • it must start with at least 2 zeros
  • 它必须至少从2个0开始
  • it may not start with more than 4 zeros
  • 开始时可能不超过4个零

I can use \d{11} to match for the exactly 11 digits, and ^0{2,4] to match the leading zeros part, but I can't figure out how to combine them.

我可以使用\ d { 11 }匹配11位数,^ 0 { 2,4]匹配前导零的部分,但我不知道如何将它们组合起来。

1 个解决方案

#1


3  

^00(?!000)\d{9}$

It checks for two zeroes and then checks that there are not more than 2 0's following it.
if it is not it checks the other 9 numbers to the end of the string.

它检查两个0,然后检查后面不超过2个0。如果不是,它会检查字符串末尾的其他9个数字。

This assumes your engine supports lookaheads.

这假定您的引擎支持lookahead。

#1


3  

^00(?!000)\d{9}$

It checks for two zeroes and then checks that there are not more than 2 0's following it.
if it is not it checks the other 9 numbers to the end of the string.

它检查两个0,然后检查后面不超过2个0。如果不是,它会检查字符串末尾的其他9个数字。

This assumes your engine supports lookaheads.

这假定您的引擎支持lookahead。