Regex where a number can start with 9, but not 999 sequentially

I am trying to create a regex, where:

  • the number can start with 3,5,6 or 9
  • the number cannot begin with 999.

Thus, for example, it is 93214211mapped, but 99912345should not be mapped.

This is what I have now that satisfies the first requirement:

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

I was stuck on second demand for a while. Thank!

+4
source share
1 answer

You can use negative lookaheadas

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

Regex Demo

Regular Expression Distribution

^ #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
+6
source

All Articles