Pattern p = Pattern.compile("(?<=st)");
Matcher m = p.matcher("testtest123");
StringBuffer sb = new StringBuffer();
while(m.find())
m.appendReplacement(sb, "\r\n");
m.appendTail(sb);
System.out.println(sb.toString());
(?<=)负向预查
(?=)正向预查
appendReplacement()
实现非终端添加和替换步骤。
appendTail()
实现终端添加和替换步骤。
此方法从添加位置开始从输入序列读取字符,并将其添加到给定字符串缓冲区。可以在一次或多次调用 appendReplacement 方法后调用它来复制剩余的输入序列。
本文介绍了如何使用正则表达式的预查功能进行文本匹配,并详细解释了Pattern和Matcher类中的appendReplacement及appendTail方法的具体用途,展示了如何通过这些方法实现文本的精确替换。
7480

被折叠的 条评论
为什么被折叠?



