Regex,其中一个数字可以以9开头,但不能连续以999开头


Regex where a number can start with 9 but not 999 consecutively

我正在尝试生成一个正则表达式,其中:

  1. 数字可以从3,5,6或9开始
  2. 号码不能以999开头

因此,例如,93214211被匹配,但是99912345不应该被匹配。

这就是我现在所拥有的,它满足了第一个要求:

^3|^5|^6|^9|[^...]}

我在第二个要求上被卡住了一段时间。谢谢

您可以像一样使用negative lookahead

^(?!999)[3569]'d{7}$ <-- assuming the number to be of 8 digits

Regex演示

Regex细分

^ #Start of string
  (?!999) #Negative lookahead. Asserts that its impossible to match 999 in beginning
  [3569] #Match any of 3, 5, 6 or 9
  'd{7} #Match 7 digits
$ #End of string
相关文章: