正则表达式,其中数字可以连续以 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 Demo

正则表达式分解

^ #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