Java case insensitive regular expressions

If like me, you need to parse some text using regular expressions and it must be done in a casi insensitive way, here is the trick you are looking for.

If you use the Pattern class approach, then add the Pattern.CASE_INSENSITIVE flag:

Pattern p = Pattern.compile("YOUR_REGEX", Pattern.CASE_INSENSITIVE);

If you prefer to use the String.matches() method, then use the ?i flag in the regular expression:

"XYZxyz".matches("(?i)[a-z]+")

I want to note I found this help on: http://blogs.sun.com/xuemingshen/entry/case_insensitive_matching_in_java.