Alfonso Khiew - 1 year ago 43

PHP Question

I'm trying to make a regex where:

- a number can start from 3,5,6 or 9
- the number cannot be starting with 999.

Thus for example,

`93214211`

`99912345`

This is what I have for now which satisfies the first requirement:

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

I'm stuck at the 2nd requirement for a while now.

Thanks!

Answer

You can use `negative lookahead`

like

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

*Regex Breakdown*

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

Source (Stackoverflow)