1

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.

1
  • 1
    You never need {1}. Everything matches once unless you quantify it. Commented Jul 26, 2021 at 20:17

1 Answer 1

2

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.

Sign up to request clarification or add additional context in comments.

3 Comments

A smarter way than I thought. Thanks +1
Good one. Unless I'm mistaken you don't need the ?:
@AbraCadaver Right. I'm so used to using the $match argument.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.