2012-02-23 54 views
0

我需要使用正則表達式和來自rss提要的字符串。它是下面的字符串。java中的轉義序列

private String text = "<![CDATA[<table border="0" cellspacing="0" cellpadding="6px"> <tr><td valign="top" align="center" width="150px"><a href="http://entertainment.desktopnexus.com/wallpaper/978620/"><img src="http://static.desktopnexus.com/thumbnails/978620-thumbnail.jpg" border="1" style="border: 1px solid #000000;"></a><br><strong>Princess,Aurora,Sleeping,Beauty</strong></td><td valign="top" style="font-size: 10pt;">A new wallpaper has been posted to the <a href="http://entertainment.desktopnexus.com">Entertainment</a> gallery on <a href="http://www.desktopnexus.com">Desktop Nexus</a>.<br><br>Uploaded By: <a href="http://my.desktopnexus.com/Jessowey/">Jessowey</a><br>Category: <a href="http://entertainment.desktopnexus.com/cat/movies/">Movies</a><br>Date Uploaded: 02/23/12<br>Native Resolution: 1024x768<br>Points: +1<br>Download Count: 0<br><br><b><a href="http://entertainment.desktopnexus.com/wallpaper/978620/">View This Wallpaper Now</a></b></td></tr></table>]]>"; 

正如你可以看到有「裏面的字符串,所以我不能用它作爲一個聲明。我試圖用原始字符串,但你也知道這是不可能在Java中。

我怎樣才能從上面的語句中提取img標籤,我需要以編程方式做到這一點。

提前感謝!

回答

1

它非常非常(一般)使用正則表達式解析XML/HTML,難有是另一個post,它列出了針對Java及其s的良好XML解析器強度,我建議你使用其中的一種。

1

使用\「在一個字符串中使用」裏面。例如:

String yourFeed = "My so \"called\" String"; 

這適用於像反斜槓本身的一些其他特殊字符:

String antoherFeed = "Hello \"World\", what a nice \\ day \\ "; 

你可以分析你的RSS FEED這樣的特殊字符。像這個問題一樣: JAVA: check a string if there is a special character in it

並將它們格式化爲有效的字符串字符。

1

如果wnat在字符串在java中的文字使用"你有這樣的

String stringWithParenthesis = "text \"in parenthesis\" out "; 
+0

感謝反斜線逃避他們,但我不能寫越獄逃跑每飼料。我需要編程。 – Isuru 2012-02-23 09:52:49

+1

在Strin LITERALS中,您不必逃避飼料 - 您只需要逃跑「 - 您的飼料是什麼?您如何獲得輸入? – Hurda 2012-02-23 14:54:43