替换string中的部分字符串 关键时候还得正则出马。。。。

前端之家收集整理的这篇文章主要介绍了替换string中的部分字符串 关键时候还得正则出马。。。。前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

String startRegex = "(<|<f|<fo|<fon|<font|<font |<font c|<font co|<font col|<font colo|<font color|<font color=|<font color='|<font color='r|<font color='re|<font color='red|<font color='red'|<font color='red'>|<font color='red'>)$";
Pattern pat = Pattern.compile(startRegex);
Matcher matcher = pat.matcher(content);
while (matcher.find()) {
content = content.replaceAll(startRegex,"");
}

String endRegex = "(</f|</fo|</fon|</font)$";
Pattern pat1 = Pattern.compile(endRegex);
Matcher matcher1 = pat1.matcher(content);
while (matcher1.find()) {
content = content.replaceAll(endRegex,"</font>");
}

String regex = "(<font color='red'>.*)$"; Pattern pat2 = Pattern.compile(regex); Matcher matcher2 = pat2.matcher(content); while (matcher2.find()) { content = content + "</font>"; }

原文链接:https://www.f2er.com/regex/358674.html

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