簡體   English   中英

我似乎無法讓replaceAll工作(刪除兩個字符串之間的所有內容)

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

因此,我試圖刪除兩個字符串(包括兩個字符串)之間的所有內容,但無法弄清楚為什么我的代碼無法正常工作。 我什至在以下網站上對其進行了測試: 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>","");

都不替換字符串。 這是怎么回事?

編輯:通過將(?s)添加到match的開頭進行修正,如anubhava所說。

您需要s可以匹配s標記(DOTALL) . 換行符還:

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

但是請注意,您應該使用HTML / XML解析器來處理HTML文檔。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM