0

我编写了一个代码来从 XML 文件(Feed)中读取新闻......我必须在我的列表视图中显示每个项目的描述......我使用这段代码删除了描述标签中存在的 html 标签:

else if ("description".equals(tagName)){
                             sourcedescription= parser.nextText();
                             description=Html.fromHtml(sourcedescription).toString();
                             Log.d("msg", description);
                             feedDescription.add(description);

                         }

有些项目我成功地在没有标签的情况下显示了它的描述,即以一种可以理解的方式,但是我没有删除其他一些有 {iframe} {/iframe} 标签的项目的所有标签......我认为这个标签存在于描述标签中有“无描述”的项目

<description><![CDATA[<p>{iframe height="600"}<a href="http://admreg.yu.edu.jo/index.php?option=com_content&view=article&id=606:------20132014&catid=87:2011-01-25-18-12-08&Itemid=438">http://admreg.yu.edu.jo/index.php?option=com_content&view=article&id=606:------20132014&catid=87:2011-01-25-18-12-08&Itemid=438</a><span style="line-height: 1.3em;">{/iframe}</span></p>]]></description>

我的问题是如何使用正则表达式删除 iframe 标签?

4

4 回答 4

2

一个可能的解决方案是

    String regexp = "\\{/?iframe.*?\\}";
    String text = "<description><![CDATA[<p>{iframe height=\"600\"}<a href=\"http://admreg.yu.edu.jo/index.php?option=com_content&view=article&id=606:------20132014&catid=87:2011-01-25-18-12-08&Itemid=438\">http://admreg.yu.edu.jo/index.php?option=com_content&view=article&id=606:------20132014&catid=87:2011-01-25-18-12-08&Itemid=438</a><span style=\"line-height: 1.3em;\">{/iframe}</span></p>]]></description>";
    System.out.println(text.replaceAll(regexp, ""));

如果要删除标签 iframe 内的内容,请改用此正则表达式:

text.replaceAll("\\{iframe .*?\\}.*?\\{/iframe\\}", "")
于 2013-08-25T08:14:00.070 回答
2

使用这些正则表达式:

\{iframe[^\}]*\}   // to delete the opening tag
\{/iframe[^\}]*\}  // to delete the closing tag

这些正则表达式不会删除 iframe 中的内容。

于 2013-08-25T08:15:37.903 回答
1

注意:如果您有选择,请使用解析器。那就是说……为了快速而肮脏……

str.replaceAll("\\{/?iframe.*?\\}", "");

删除这些标签之间的内容。

str.replaceAll("\\{iframe.*?\\}.*?\\{/iframe\\}", "")
于 2013-08-25T09:01:30.023 回答
0

HTML 不是常规语言。不要将 RegEx 与它一起使用,否则你会死的。

于 2013-08-25T08:27:45.457 回答