删除两个字符串之间的每个字符串并在特定字符串后发生

时间:2016-06-13 03:35:32

标签: java string

我在Java中有一个String:

style =“hello World”>一次性会议< style = \“Hello Again”>停止“你好” Sample Input 我想删除“”之间的所有字符串,在每次出现字符串“Style”后立即发生。

因此,删除后,上面的字符串将如下所示:

style =“”>一次性会议<风格= \ “” >停止“你好”

〜感谢

2 个答案:

答案 0 :(得分:2)

如果您要删除style属性中引号之间的所有字符串,那么简单的replaceAll()应该可以解决问题:

String input = "style=\"hello World\">One-time meetings< style=\"Hello Again\"> stop \"Hello\"";
input = input.replaceAll("style=\"(.*?)\"", "style=\"\"");

<强>更新

从检查原始输入看,<style>标记内的引号本身已被单个反斜杠转义。如果是这种情况,那么以下替换应该给你你想要的东西:

String input = "style=\\\"hello World\\\">One-time meetings< style=\\\"Hello Again\\\"> stop \"Hello\"";
input = input.replaceAll("style=\\\\\"(.*?)\\\\\"", "style=\\\\\"\\\\\"?");

答案 1 :(得分:1)

我认为parsing HTML with regex is a bad idea 请使用解析器,例如JSoup

示例代码:

Document doc = Jsoup.parse(html);
doc.select(".style").attr("style", null);
String htmlWithoutStyle = doc.outerHtml();