tags:

views:

1649

answers:

7

Ok,

I need a regex for the following pattern:

-Total of 5 characters (alpha and numeric, nothing else).

-first character must be a letter (A,B,C only)

-the remaining 4 characters can be number or letter.

clarifcation: the first letter can only be A,B, or C. Example: A1234 is valid but D1234 is invalid.

+11  A: 

EDIT: Grrr... edited regex due to new "clarification" :)

^[A-C][a-zA-Z0-9]{4}$

EDIT: To explain the above Regex in English...

^ and $ mean "From start to finish" (this ensures that the whole string must perfectly match)

[A-C] means "Match either A, B or C"

[a-zA-Z0-9]{4} means "Match 4 lower case letters, upper case letters or numbers"

Timothy Khouri
NB. This will only match this pattern if it's on a separate line.
Mats Fredriksson
@mats - or in a separate variable.
Paul Tomblin
What do you mean by the seperate line comment? This regex is perfect :)
Timothy Khouri
NB. This will only match this pattern if IT IS ON A SINGLE LINE I think is what he meant
Will
since you are using ^ and $, the match will not occur if the string is in the middle of some other text. So this would not match "this is text A1234 this is more text". It is not clear whether this is ok or not from the OP.
EBGreen
Correct, the Regex will only match a perfect match for what was asked for. If he said "find this somewhere in a block of text", I would remove the ^ and $... but as I imagine this to be validation, I like it to be 'perfect'.
Timothy Khouri
That is what was meant by the seperate line and separate variable comments.
EBGreen
That was in the requirements. "Total of 5 characters (alpha and numberic [sic], nothing else)"
P Daddy
The A,B,C requirement is not met so I would say that you missed perfect.
EBGreen
I think "A,B,C" was an example :) ... still perfect!
Timothy Khouri
Well, new clarification = updated regex... perfect again!!!
Timothy Khouri
You might want to edit your explanation so that it matches the pattern.
EBGreen
When asking regular expressions questions it's always a good idea to mention the implementation. Regular expressions are anything but "regular," with each flavor offering a different set of features.
converter42
+6  A: 

Something along the lines of:

[A-C][A-Za-z0-9]{4}

I would advise taking a look at http://regexlib.com/CheatSheet.aspx if you are unfamiliar with regular expressions and try to do these kind of simple regexs yourself.

There is also plenty of online regex testing apps such as: http://regexlib.com/RETester.aspx which enable you to test your regexes without writing any code.

Martin
+2  A: 

Do you mean that the first letter must be an A, B or C? Or can it be any letter?

If it has to be an A,B,or C (case sensitive), then this would be the regular expression.

[A-C][a-zA-Z0-9]{4}

Otherwise, the other answers here suffice.

Carl
A: 

In case this is not Perl regexps we're talking about, some cut-and-paste is needed:

[ABC][a-zA-Z0-9][a-zA-Z0-9][a-zA-Z0-9][a-zA-Z0-9]

I cut-and-pasted "[a-zA-Z0-9]" four times.

Arkadiy
The regex's below work for JavaScript and .NET too... are you saying that Perl doesn't support {4}, or are you saying that other languages don't?
Timothy Khouri
@Timothy - perl supports the {count}. At one time, regular grep didn't but egrep did. So different languages have different versions of "regular expressions" depending on whether they copied from grep, egrep or perl (or something else).
Paul Tomblin
Thanks - I don't know Perl's regex in depth, so I was just wondering about it due to this answer.
Timothy Khouri
I would suggest reading Mastering Regular Expressions by Jeffrey E.F. Friedl if you want to see some comparisons of different regex engines.
EBGreen
I believe it's available online if you really want to read it
Nathan Fellman
Pretty much every regex flavor in use today supports {4}. BRE-style flavors use \{4\} instead.
Jan Goyvaerts
A: 
/[ABC](?i:[a-z0-9]{4})/
Brad Gilbert
A: 

\<[ABC]([a-zA-Z0-9]{4})\>

jmc
A: 

This answer is correct, but I did want to add that you can shorten it a bit with the following.

^[A-C]\w{4}$

\w means any alphaNumeric character.