Solved

help with regex pattern

Posted on 2012-04-02
5
250 Views
Last Modified: 2012-04-02
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.
0
Comment
Question by:-Dman100-
  • 2
  • 2
5 Comments
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 37797912
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

0
 
LVL 23

Expert Comment

by:wdosanjos
ID: 37797916
Please try the following pattern:

^((RTN)|(ADJ)|(TRA))\d{7}$
0
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 37797937
Ah, I missed the restriction on the letters. Silly me  : \
0
 
LVL 41

Accepted Solution

by:
HonorGod earned 500 total points
ID: 37797958
You don't need the extra parenthesis

^(RTN|ADJ|TRA)\d{7}$

Open in new window

0
 
LVL 41

Expert Comment

by:HonorGod
ID: 37798665
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.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Regex Expression Needed to Parse FTP LIST to Isolate just the Filename 3 44
Help with powershell and regex 5 46
Specific format 21 181
Regular Expression 1-100 with two digits 15 22
by Batuhan Cetin Regular expression is a language that we use to edit a string or retrieve sub-strings that meets specific rules from a text. A regular expression can be applied to a set of string variables. There are many RegEx engines for u…
As most anyone who uses or has come across them can attest to, regular expressions (regex) are a complicated bit of magic. Packed so succinctly within their cryptic syntax lies a great deal of power. It's not the "take over the world" kind of power,…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…

911 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now