I need regex patterns for the AANNN
and ANEE
formats.
A
means lettersN
means digitsE
means either letters or digits.
I need to validate input strings according to these formats.
Examples
BD123
matches theAANNN
G21H
matches theANEE
I need regex patterns for the AANNN
and ANEE
formats.
A
means lettersN
means digitsE
means either letters or digits.I need to validate input strings according to these formats.
Examples
BD123
matches the AANNN
G21H
matches the ANEE
I'm assuming you want ASCII letters and digits, but not Thai digits, Arabic letters and the like.
AANNN
is (?i)^[a-z]{2}[0-9]{3}$
ANEE
is (?i)^[a-z][0-9][a-z0-9]{2}$
Explanation
^
anchor asserts that we are at the beginning of the string[a-z]
is what's called a character class. It allows any chars between a
and z
. E
is [a-z0-9]
A
you would have to say something like [a-zA-Z]
, but the (?i)
flag makes it case-insensitive{2}
is a quantifier that means "match the previous expression twice$
anchor asserts that we are at the end of the stringHow to Use
In Java, you can do something like:
if (subjectString.matches("(?i)^[a-z]{2}[0-9]{3}$")) {
// It matched!
}
else { // nah, it didn't match... }
Regular Expressions are Fun! I highly recommend you go learn from one of these resources.
Resources