我有一个字符串如下:
This is a sample <b>String</b> I want to <div style="font-size:22px"> replace it fast </div>
如何一次性从Java中的上述字符串中删除<div>
开始和</div>
结束标记?
答案 0 :(得分:15)
你可以这样做:
String s = "This is a sample <b>String</b> I want to <div style=\"font-size:22px\"> replace it fast </div>";
s = s.replaceAll("[<](/)?div[^>]*[>]", "");
这将导致:
This is a sample <b>String</b> I want to replace it fast
答案 1 :(得分:2)
尝试以下方法:
myString.replaceAll("<div .*>(.*)</div>","$1");
btw:有一个java的在线正则表达式测试器:
http://www.regexplanet.com/simple/index.html
(如果你不知道的话)
希望有所帮助
答案 2 :(得分:2)
你想要哪一个?
String s = "This is a sample <b>String</b> I want to <div style=\"font-size:22px\"> replace it fast </div>";
// if you want to remove only div tag.
System.out.println(s.replaceAll("</?div[^>]*?>", ""));
// if you want to remove tag include text.
System.out.println(s.replaceAll("<div[^>]*?>.*?</div[^>]*?>", ""));
// language: java