[Solved] Regex patterns for AANNN and ANEE formats [closed]


I’m assuming you want ASCII letters and digits, but not Thai digits, Arabic letters and the like.

  1. AANNN is (?i)^[a-z]{2}[0-9]{3}$
  2. ANEE is (?i)^[a-z][0-9][a-z0-9]{2}$

Explanation

  • The ^ 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.
  • In a character class we can include several ranges, that’s why the E is [a-z0-9]
  • Normally for 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
  • The $ anchor asserts that we are at the end of the string

How 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

  • Regex FAQ on Stack Overflow.
  • Character Classes
  • Mastering Regular Expressions, 3rd Ed
  • The Regular Expressions Cookbook, 2nd Ed

5

solved Regex patterns for AANNN and ANEE formats [closed]