所以我试图删除两个字符串(包括两个字符串)之间的所有内容,但我无法弄清楚为什么我的代码不起作用。我什至在这个网站上测试过它:http://www.regexplanet.com/advanced/java/index.html
他们的“replaceAll()”部分具有我所期望的确切字符串,但我在我的 Android 应用程序上得到的是完整的原始字符串,没有任何变化。我根据本网站上的示例尝试了不同的方法,例如:
String thematch = Pattern.quote("<p>Listen") + "(.*?)" + Pattern.quote("</audio></p>");
String thenew = thecontent.replaceAll(thematch, "");
和:
String thenew = thecontent.replaceAll("<p>Listen(.*?)</audio></p>","");
两者都没有替换字符串。怎么回事?
编辑:如 anubhava 所说,通过在匹配项的开头添加 (?s) 来修复。
最佳答案
您需要 s
标志 (DOTALL) 来匹配 。
换行符:
String thematch = "(?s)" + Pattern.quote("<p>Listen") + "(.*?)" + Pattern.quote("</audio></p>");
但是请注意,您应该使用 HTML/XML 解析器来处理 HTML 文档。
关于java - 我似乎无法让 replaceAll 工作(删除两个字符串之间的所有内容),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25266880/