java中的转义序列
我需要对来自 rss feed 的字符串使用正则表达式。它是下面的字符串。
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 标签。我需要
以编程方式完成此操作,谢谢!
I need to use regular expression with a string from a rss feed. It is following string.
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>]]>";
As you can see there are " inside the string, so I can't use it as a statement. I tried to use raw string but as you know it is not possible in java.
How can I extract img tag from the above statement. I need to do it programatically.
Thanks in advance!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
(一般来说)使用正则表达式来解析 XML/HTML 是非常非常困难的,还有另一个 这篇文章列出了优秀的 Java XML 解析器及其优点,我建议您使用其中之一。
It's very, very difficult (in general) to use regular expressions to parse XML/HTML, there is another post which lists good XML parsers for Java and their strengths, I suggest you use one of these.
如果你想在java中的字符串文字中使用
"
你必须像这样用反斜杠转义它们If you wnat to use
"
in String literal in java you have to escape them with backslash like that在字符串中使用 \" 即可使用其中的 "。例如:
这适用于其他一些特殊字符,例如反斜杠本身:
您可以解析 RSS-FEED 来查找此类特殊字符。就像这个问题一样:
JAVA:检查字符串是否存在其中的特殊字符
并将其格式化为有效的字符串字符。
Use \" in a string to use the " inside it. For example:
This works for some other special character like the backslash itself:
You can parse your RSS-FEED for such special characters. Like in this question:
JAVA: check a string if there is a special character in it
and format them to valid string characters.