views:

135

answers:

2

It would seem that regular expression questions are common as each question seems to apply to a specific case. With that said, here is my specific case...

I need to validate that an employee id is in one of two specific formats. The first format (and one that I have working reliably) is (w/o the quotes): "A000000" , where "A" can be any alphabet character and "0" can be any number between 0 and 9. The other requirement for this is that only the first two positions of the employee id are required, meaning that a user can enter "A0" and it would validate (this employee id gets used in a SQL statement's LIKE - ...SID LIKE "A0%". Again, this I have by using...

[A-Z]{1}\d{1,6}

The second format is the one giving me trouble. The same requirement holds above where only the first two positions of the input string are required, anything else is optional, but if entered it must adhere to the specified format. The format is (w/o the quotes): "000000A". The part I am having trouble with is making sure that if more than two digits are entered, that the next three to six values entered are digits, and if the last value is entered, then it must be a character. So valid inputs could be...

"00" or "000" or "0000" or "00000" or "000000" or "000000A", but NOT "0" or "0A" or "00A", or "000A" etc., or "0000000". Here is what I have so far for this format...

\d{2}(\d{1,4})?[A-Z]{0,1}

The problem is that this allows alpha-characters to be entered anywhere in positions three through six. It will not allow any alpha-characters in the first two positions (good) and it will not allow any numeric characters in the seventh (last) position (good). Its just those optional third through sixth characters allowing alpha-characters that is causing me grief.

I hope this is enough info (or perhaps not too much). Any help would be greatly appreciated. Thank you!!!

A: 

For the first one, the {1} is redundant:

[A-Z][0-9]{1,6}

For the 2nd, maybe this:

\d{2}\d{0,1}\d{0,1}\d{0,1}\d{0,1}[A-Z]{0,1}

Joel Coehoorn
This won’t work. It will also allow 00A.
Gumbo
+2  A: 

Try this expression:

(\d{2,5}|\d{6}[A-Z]?)

This matches either 2 to 5 digits (\d{2,5}) or six digits and an optional letter (\d{6}[A-Z]?). And if you want an exact match, use the ^ and $ anchors to mark the begin and end:

^(\d{2,5}|\d{6}[A-Z]?)$
Gumbo
This worked great, thank you very much (especially for the quick response)