2013-08-25 90 views
0

我寫了一個代碼來讀取來自XML文件(Feed)的新聞..我必須在我的列表視圖中顯示每個項目的描述...並且我用這個代碼豌豆去除html標籤說明標籤內部存在:如何使用正則表達式從java中的字符串中刪除一些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標記?

+2

爲什麼是正則表達式?爲什麼不使用適當的工具來完成這項工作,比如XPath? – EJP

+1

http://stackoverflow.com/questions/1732348/regex-match-open-tags-except-xhtml-self-contained-tags/1732454#1732454 –

回答

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\\}", "") 
2

使用這些正則表達式:

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

這些正則表達式不會刪除iframe中的內容。

0

HTML是不是一個正規的語言。不要使用RegEx,否則你會死的。

+0

好吧,我會盡力不再做。 –

+0

大聲笑這是一個有趣的評論! –

+0

@SameerSawla這是一個答案刪除評論 –

1

注意:如果您有選項,請使用解析器。這就是說...對於一個快速和髒...

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

要刪除這些標記之間的內容。

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

好這項工作..謝謝:) ,,但是在此標籤內還出現了:http://admreg.yu.edu.jo/index.php?option=com_content&view=文章&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 – Akari

+0

如何將其刪除? – Akari

+0

我的意思是的內容,即href的值!! – Akari

相關問題