Possible Duplicate:
How to remove HTML tag in Java
RegEx match open tags except XHTML self-contained tags
我想删除特定的 HTML 标记及其内容。
例如,如果 html 是:
<span style='font-family:Verdana;mso-bidi-font-family:
"Times New Roman";display:none;mso-hide:all'>contents</span>
如果标签包含“mso-*”,则必须删除整个标签(开头、结尾和内容)。
最佳答案
如Dave Newton在他的评论中指出,html 解析器是解决问题的方法。如果您确实想以困难的方式做到这一点,这里有一个有效的正则表达式:
String html = "FOO<span style='font-family:Verdana;mso-bidi-font-family:"
+ "\"Times New Roman\";display:none;mso-hide:all'>contents</span>BAR";
// regex matches every opening tag that contains 'mso-' in an attribute name
// or value, the contents and the corresponding closing tag
String regex = "<(\\S+)[^>]+?mso-[^>]*>.*?</\\1>";
String replacement = "";
System.out.println(html.replaceAll(regex, replacement)); // prints FOOBAR
关于JAVA正则表达式删除html标签和内容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14124207/