简体   繁体   English

我似乎无法让replaceAll工作(删除两个字符串之间的所有内容)

[英]I can't seem to get replaceAll to work (remove all content between two strings)

So I am trying to remove all the content between two strings (including the two strings) and I can't figure out why my code is not working. 因此,我试图删除两个字符串(包括两个字符串)之间的所有内容,但无法弄清楚为什么我的代码无法正常工作。 I even tested it on this website: http://www.regexplanet.com/advanced/java/index.html 我什至在以下网站上对其进行了测试: http : //www.regexplanet.com/advanced/java/index.html

And their "replaceAll()" section has the exact string I'm expecting, but what I get on my Android app is the entire original string with no changes. 他们的“ replaceAll()”部分具有我期望的确切字符串,但是我在Android应用程序中得到的是没有更改的整个原始字符串。 I have tried different methods based on examples found on this site such as: 我根据此站点上的示例尝试了不同的方法,例如:

String thematch = Pattern.quote("<p>Listen") + "(.*?)" + Pattern.quote("</audio></p>");
String thenew = thecontent.replaceAll(thematch, "");

And: 和:

String thenew = thecontent.replaceAll("<p>Listen(.*?)</audio></p>","");

Neither are replacing the string. 都不替换字符串。 What's going on? 这是怎么回事?

Edit: Fixed by adding (?s) to the beginning of thematch as anubhava said. 编辑:通过将(?s)添加到match的开头进行修正,如anubhava所说。

You need s flag (DOTALL) that lets you match . 您需要s可以匹配s标记(DOTALL) . newlines also: 换行符还:

String thematch = "(?s)" + Pattern.quote("<p>Listen") + "(.*?)" + Pattern.quote("</audio></p>");

However as a word of caution you should use a HTML/XML parser for manipulating HTML documents. 但是请注意,您应该使用HTML / XML解析器来处理HTML文档。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM