替换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> (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> (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;
已检查:
PS:这只是一个示例,输入可能会有所不同。您的regexp用于html标记
将被匹配,而html实体将不匹配。他们的模式类似于&.*代码>未替换的
这将解决您的问题:
str = str.replaceAll("\\<.*?\\>|&.*?;", "");
str=str.replaceAll(“\\\\\&.*?”,”);
如果您想在沙箱中进行实验,请尝试regxr.com并使用(\)|(&.*?)
括号使两个不同的捕获组在工具上易于识别,并且在代码中不需要。请注意,\
不需要在沙箱操场上转义,但它必须在您的代码中,因为它在字符串中。您的regexp用于hml标记,html实体将匹配类似于&.*代码>您不在重放的代码trystr=str.replaceAll(“\\\\\\\&.*;”,”)上面的代码>删除HTML标记后仅返回:发送FWB
我错了:str=str.replaceAll(“\\\\\&.*?”,”)代码>应该可以工作。在机器上测试,效果非常好。