正则表达式--appendReplacement

前端之家收集整理的这篇文章主要介绍了正则表达式--appendReplacement前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
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?

猜你在找的正则表达式相关文章