import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class MainClass {
public static void main(String[] args) {
Pattern pattern = Pattern.compile("java",Pattern.CASE_INSENSITIVE);
String string = "java Java JAva JAVa JAVA I fuck Java,why?";
Matcher matcher = pattern.matcher(string);
StringBuffer sb1 = new StringBuffer();
while (matcher.find()) {
matcher.appendReplacement(sb1,"JAVA");
}
matcher.appendTail(sb1);
System.out.println(sb1);
matcher.reset();
StringBuffer sb2 = new StringBuffer();
while (matcher.find()) {
matcher.appendReplacement(sb2,"java");
}
matcher.appendTail(sb2);
System.out.println(sb2);
}
}
output:
JAVA JAVA JAVA JAVA JAVA I fuck JAVA,why?
java java java java java I fuck java,why?