Saturday, May 25, 2013

Howto make regular expressions caseinsensitive

Regular expression are very useful for string operations, but one annoying thing is that regular expressions are normally case sensitive. But with a simple option you can change this default behavior. You only have to add the "(?i)" clause to your regular expression. Here is an example:

  
  String test = "The lazy brown fox jumps over the quick Dog";
  // The following command will not replace Dog, because the regex is not
  // case insensitive.
  System.out.println(test.replaceAll("dog", "cat"));
  // With (?i) added the regex will ignore case and Dog will be replaced
  // by cat.
  System.out.println(test.replaceAll("(?i)dog", "cat"));


But be aware, that the "(?i)" clause does only work for ASCII strings. If you have international characters in your regular expressions you have to add the "u" flag to your regex.

 
  String test = "Äpfel und Birnen";
  // The following command will not replace Äpfel, because the regex is
  // not unicode aware.
  System.out.println(test.replaceAll("(?i)äpfel", "Zitronen"));
  // With (?iu) added the regex is unicode aware and Äpfel will be
  // replaced by Zitronen.
  System.out.println(test.replaceAll("(?iu)äpfel", "Zitronen"));

No comments:

Post a Comment

ad