Saleh Ahmad Oyon Saleh Ahmad Oyon - 6 months ago 20
PHP Question

Regex pattern can't figure out

I need a regex pattern for php. It will only accept the following two patterns.

XXXX-XXXX-2
XXXX-XXX-3


Here, X = digit

For example, It will accept,

1111-1111-2
1234-567-3
7894-753-2
5287-6325-3


But it won't accept,

526-5248-2
7895-4125-1
7854-856-4
asas-wewe-y


and so on.

What will be the regular expression for this type of pattern ?

Answer

This will work

^\d{4}-\d{3,4}-[23]$

Regex Demo

Regex Breakdown

^ #Start of string
 \d{4} #Match 4 digits
  - #Match - literally
  \d{3,4} #Match 3 or 4 digits
  - #Match - literally
  [23] #Match 2 or 3
$ #End of string
Comments