使用文本节点的以下值...
MatcH one MatcHer two MarcH three
如何使用 java matcher.find() 创建以下输出?
<wrap>MatcH</wrap> one MatcHer two <wrap>MarcH</wrap> three
假设一个 Java 正则表达式捕获所有以大写“M”开头并以大写“H”结尾的单词
\bM\w*H\b
基本上,我想用包装标签包围任何与这个正则表达式匹配的东西
String text = "MatcH one MatcHer two MarcH three";
Pattern pattern = Pattern.compile(\\bM\w*H\b\);
Matcher matcher = pattern.matcher(text);
// replace each time the regex is found
while (matcher.find()) {
text = text.replaceAll(matcher.group(), "<wrap>" +
+ matcher.group() + "</wrap>");
}
ReplaceFirst/ReplaceAll 对我不起作用,因为它会导致以下结果......
<wrap>MatcH</wrap> one <wrap>MatcH</wrap>er two <wrap>MarcH</wrap> three
提前致谢...