【发布时间】:2020-12-14 22:18:27
【问题描述】:
如果不使用StringEscapeUtils,我怎样才能达到低于预期的结果?
public class Main {
public static void main(String[] args) throws Exception {
String str = "<p><b>Send FWB <br><br> (if AWB has COU SHC, <br> if ticked , will send FWB)</b></p>";
str = str.replaceAll("\\<.*?\\>", "");
System.out.println("After removing HTML Tags: " + str);
}
}
当前结果:
After removing HTML Tags: Send FWB (if AWB has COU SHC, if ticked , will send FWB)
预期结果:
After removing HTML Tags: Send FWB if AWB has COU SHC, if ticked , will send FWB;
已检查: How to unescape HTML character entities in Java?
PS:这只是一个示例,输入可能会有所不同。
【问题讨论】:
-
您的正则表达式用于 hml 标签
html 实体将匹配您未替换的 &.*;之类的内容 -
试试
str = str.replaceAll("\\<.*?\\>|&.*;", ""); -
以上只返回
After removing HTML Tags: Send FWB -
我错了:
str = str.replaceAll("\\<.*?\\>|&.*?;", "");应该可以。在regexr.com 上对其进行了测试
标签: java html string replace ascii