SweetNGX
SweetNGX

Reputation: 163

PHP preg_match 1 string 2 digit validation

value: a11b22c33

I want to validate above value with preg_match with 1 string and 2 numbers

preg_match('/^\s{1}\d{2}\s{1}\d{2}\s{1}\d{2}$/', $value)

I tried but it didn't work.

Upvotes: 1

Views: 555

Answers (1)

Barmar
Barmar

Reputation: 781004

Use this:

preg_match('/^([a-z]\d{2}){3}$/', $value)

\s matches whitespace, [a-z] matches lowercase letters. If you also need uppercase letters, add the i modifier after the second / to make it case-insensitive.

Since you're repeating the same pattern 3 times, I've grouped it and used the {3} quantifier rather than writing it out 3 times.

Upvotes: 2

Related Questions