java中的转义序列

发布于 2025-01-08 07:52:01 字数 1398 浏览 2 评论 0原文

我需要对来自 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

夏了南城 2025-01-15 07:52:01

(一般来说)使用正则表达式来解析 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.

幸福丶如此 2025-01-15 07:52:01

如果你想在java中的字符串文字中使用 " 你必须像这样用反斜杠转义它们

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

If you wnat to use " in String literal in java you have to escape them with backslash like that

String stringWithParenthesis = "text \"in parenthesis\" out ";
眼眸 2025-01-15 07:52:01

在字符串中使用 \" 即可使用其中的 "。例如:

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

这适用于其他一些特殊字符,例如反斜杠本身:

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

您可以解析 RSS-FEED 来查找此类特殊字符。就像这个问题一样:
JAVA:检查字符串是否存在其中的特殊字符

并将其格式化为有效的字符串字符。

Use \" in a string to use the " inside it. For example:

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

This works for some other special character like the backslash itself:

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

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文