Link to home
Start Free TrialLog in
Avatar of -Dman100-
-Dman100-Flag for United States of America

asked on

help with regex pattern

I'm terrible with regular expressions and cannot figure out how to use a regex expression to check for the following pattern.

The first 3 characters must be alpabetic and the next 7 characters must be numeric.

The first 3 characters can only be:
RTN
ADJ
TRA

The remaining 7 characters can be any numeric value 0-9

I'm sure this is simple, but I can't figure it out.

I was starting with this:
[A-Za-z][A-Za-z][A-Za-z][0-9] but, this isn't even close.

Thanks for any help.
Avatar of kaufmed
kaufmed
Flag of United States of America image

Actually, it is close--it's just not finished  = )

I believe you just need to repeat the digit part 6 more times:

[A-Za-z][A-Za-z][A-Za-z][0-9][0-9][0-9][0-9][0-9][0-9][0-9]

Open in new window


However, if you can tell us what language or editor you are using, we might be able to shorten it a bit. For example, you might be able to use:

[A-Za-z]{3}[0-9]{7}

Open in new window


Also, you may need some boundaries on that pattern to prevent matching substrings. Start of string ( ^ ) and end of string ( $ ) are fairly common among languages/editors:

^[A-Za-z][A-Za-z][A-Za-z][0-9][0-9][0-9][0-9][0-9][0-9][0-9]$

Open in new window

Please try the following pattern:

^((RTN)|(ADJ)|(TRA))\d{7}$
Ah, I missed the restriction on the letters. Silly me  : \
ASKER CERTIFIED SOLUTION
Avatar of HonorGod
HonorGod
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
wdosanjos provided the 1st answer, I just simplified it.

Please "request attention" and have them reopen it so that you can give most of the points to wdosanjos.

Thanks!

Good luck & have a super day.