替换Java中的ASCII代码和HTML标记

替换Java中的ASCII代码和HTML标记,java,html,string,replace,ascii,Java,Html,String,Replace,Ascii,如果不使用StringEscapeUtils,如何实现以下预期结果 public class Main { public static void main(String[] args) throws Exception { String str = "<p><b>Send FWB <br><br> &#40;if AWB has COU SHC, <br> if ticked , will send

如果不使用
StringEscapeUtils
,如何实现以下预期结果

public class Main {
    public static void main(String[] args) throws Exception {
      String str = "<p><b>Send FWB <br><br> &#40;if AWB has COU SHC, <br> if ticked , will send FWB&#41;</b></p>";
      str = str.replaceAll("\\<.*?\\>", "");
      System.out.println("After removing HTML Tags: " + str);
    }
}
预期结果:

After removing HTML Tags: Send FWB  &#40;if AWB has COU SHC,  if ticked , will send FWB&#41;
After removing HTML Tags: Send FWB  if AWB has COU SHC,  if ticked , will send FWB;
已检查:



PS:这只是一个示例,输入可能会有所不同。

您的regexp用于html标记
将被匹配,而html实体将不匹配。他们的模式类似于
&.*未替换的

这将解决您的问题:

str = str.replaceAll("\\<.*?\\>|&.*?;", "");
str=str.replaceAll(“\\\\\&.*?”,”);

如果您想在沙箱中进行实验,请尝试regxr.com并使用
(\)|(&.*?)
括号使两个不同的捕获组在工具上易于识别,并且在代码中不需要。请注意,
\
不需要在沙箱操场上转义,但它必须在您的代码中,因为它在字符串中。

您的regexp用于hml标记,html实体将匹配类似于
&.*您不在重放的代码try
str=str.replaceAll(“\\\\\\\&.*;”,”)删除HTML标记后仅返回
:发送FWB
我错了:
str=str.replaceAll(“\\\\\&.*?”,”)应该可以工作。在机器上测试,效果非常好。