简体   繁体   中英

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

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. 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.

You need s flag (DOTALL) that lets you match . 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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