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
, or C
only)
the remaining 4 characters can be number or letter.
Clarifcation: the first letter can only be A
, B
, or C
.
Examples:
A1234
is validD1234
is invalidMost characters, including all letters ( a-z and A-Z ) and digits ( 0-9 ), match itself. For example, the regex x matches substring "x" ; z matches "z" ; and 9 matches "9" . Non-alphanumeric characters without special meaning in regex also matches itself. For example, = matches "=" ; @ matches "@" .
In regular expressions, the hyphen ("-") notation has special meaning; it indicates a range that would match any number from 0 to 9. As a result, you must escape the "-" character with a forward slash ("\") when matching the literal hyphens in a social security number.
[a-zA-Z0-9_]+ Matches alpha-numeric character and underscore.
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"
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With