Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Regular Expression to check string is in particular format

Tags:

regex

can someone please help me to compose a regular expression to check an alphanumeric string is in a particular format.

First character must be a letter and the next 6 characters are numbers...eg x279833 or X279833 are both valid.

This is what i've come up with - ^[A-Za-z]{1}[0-9]{6}$

regards

like image 205
Kojof Avatar asked Dec 16 '22 23:12

Kojof


1 Answers

Yours should work just fine (you edited it in after I wrote this answer), but the {1} is completely unnecessary. You can shorten it a little to use \d instead of [0-9].

If you want to make sure the entire string is that format, use:

^[a-zA-Z]\d{6}$
like image 140
Andy E Avatar answered Mar 23 '23 21:03

Andy E