Robert Smith
Robert Smith

Reputation: 779

One or Two Letters followed by 3-4 numbers

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

Upvotes: 4

Views: 8724

Answers (3)

aurya
aurya

Reputation: 367

I think this will fit your need

[^a-zA-Z0-9]?[a-zA-Z]{1,2}[0-9]{3,5}[a-zA-Z]?[^a-zA-Z0-9]?

Upvotes: 0

Sobrique
Sobrique

Reputation: 53508

go on, give it a try. Look at http://regex101.com and it's actually quite easy.

^[^A-Za-z]*[A-Za-z]{1,2}[0-9]{3,5}.?[^A-Za-z]*

I'd normally suggest using an 'i' (for case insenstive) flag if your RE actually is.

E.g.:

https://regex101.com/r/sR7xG8/1

Upvotes: 0

anubhava
anubhava

Reputation: 786091

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

Upvotes: 12

Related Questions