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