Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

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!

share|improve this question

1 Answer 1

up vote 4 down vote accepted
^[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
share|improve this answer
    
\d can be used as a shortcut for [0-9] as well. –  Amber Sep 2 '10 at 2:44
    
@Amber Yup, I was going for something as easy to understand as possible here though. :) –  deceze Sep 2 '10 at 2:47

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.