正则表达式实现条件替换功能
Pattern px = Pattern.compile("java", Pattern.CASE_INSENSITIVE);
Matcher mx = px.matcher("java JAVA JavaJavaJAVA java llala jaVa");
System.out.println(mx.replaceAll("JAVA"));
mx.reset();
int cnt = 0;
StringBuffer sb = new StringBuffer();
while(mx.find()){
if(++cnt % 2 == 1)
mx.appendReplacement(sb, "java");
else mx.appendReplacement(sb, "JAVA");
}
mx.appendTail(sb);
System.out.println(sb);
本文来自博客园,作者:hjzqyx,转载请注明原文链接:https://www.cnblogs.com/hujunzheng/p/4082225.html