Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/334.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
JAVA正则表达式删除html标记和内容_Java_Html_Regex - Fatal编程技术网

JAVA正则表达式删除html标记和内容

JAVA正则表达式删除html标记和内容,java,html,regex,Java,Html,Regex,可能重复: 我想删除特定的HTML标记及其内容 例如,如果html为: <span style='font-family:Verdana;mso-bidi-font-family: "Times New Roman";display:none;mso-hide:all'>contents</span> 内容 如果标记包含“mso-*”,则必须删除整个标记(开始、结束和内容)。正如他在评论中指出的那样,html解析器就是解决方法。如果您真的想以艰难的方式完成,下面是

可能重复:

我想删除特定的HTML标记及其内容

例如,如果html为:

<span style='font-family:Verdana;mso-bidi-font-family:
"Times New Roman";display:none;mso-hide:all'>contents</span>
内容
如果标记包含“mso-*”,则必须删除整个标记(开始、结束和内容)。

正如他在评论中指出的那样,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
String html=“FOOcontentsBAR”;
//正则表达式匹配属性名中包含“mso-”的每个开始标记
//或值、内容和相应的结束标记
字符串regex=“]+?mso-[^>]*>.*”;
字符串替换=”;
System.out.println(html.replaceAll(regex,replacement));//打印FOOBAR

就我个人而言,我会使用HTML解析器。这些类型的问题可能重复,在这里没有被多次询问过吗?如果style属性不包含任何
mso-
指令。。。也许需要一个更通用的regexp。@pap让我引用一下OP:如果标记包含“mso-*”,它必须删除整个标记(开始、结束和内容)。我的帖子回答了他的问题,我不理解你的评论。事实上你是对的。我为没有正确阅读这个问题而感到羞耻:)我认为你低估了自己,你似乎很好地理解了我的评论,只是我错了;)@爸爸,这是我礼貌的说法,我认为你的评论是错误的;)