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);
本文转自 小眼儿 博客园博客,原文链接:http://www.cnblogs.com/hujunzheng/p/4082225.html,如需转载请自行联系原作者