Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

One or Two Letters followed by 3-4 numbers

Tags:

regex

I am trying to find the correct RegEx pattern to allow one or two letters followed by 3 to 5 numbers and optional One letter at the end. Finally non-alphanumeric should be allowed to wrap the string:

Allowed
M394
,MP4245)
TD493!
X4958A
V49534@
U394U
A5909.

Not Allowed
TED492
R32
R4!3
U394UU
A5909AA
5349A

I found an example but it does not quite work:

RegEx pattern any two letters followed by six numbers

Thanks for your help

like image 592
Robert Smith Avatar asked Dec 15 '22 13:12

Robert Smith


1 Answers

You can use this regex:

\b[a-zA-Z]{1,2}\d{3,5}[a-zA-Z]?\b

RegEx Demo

Breakup of regex

\b             # word boundary
[a-zA-Z]{1,2}  # 1 or 2 letters
\d{3,5}        # 3 to 5 digits
[a-zA-Z]?      # an optional letter
\b             # word boundary
like image 106
anubhava Avatar answered Dec 30 '22 18:12

anubhava