1

I am looking for a PHP regex to check a string is:

  • 4 characters long
  • the 1st character is A-Z (capitalized alphabet)
  • the 2nd to 4th characters are numbers (0-9)

A good example is: A123

Thanks!

1 Answer 1

4
^[A-Z][0-9]{3}$

Explanation:

^         # start of string
[A-Z]     # one character, A-Z
[0-9]{3}  # three characters, 0-9
$         # end of string
Sign up to request clarification or add additional context in comments.

2 Comments

\d can be used as a shortcut for [0-9] as well.
@Amber Yup, I was going for something as easy to understand as possible here though. :)

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.