简体   繁体   English

Java中的转义序列

[英]Escape sequence in java

I need to use regular expression with a string from a rss feed. 我需要对来自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. 如您所见,字符串中包含“”,所以我不能将其用作语句。我尝试使用原始字符串,但是您知道在Java中是不可能的。

How can I extract img tag from the above statement. 如何从上述语句中提取img标签。 I need to do it programatically. 我需要以编程方式进行。

Thanks in advance! 提前致谢!

通常,使用正则表达式解析XML / HTML非常非常困难,还有另一篇文章列出了适用于Java的优秀XML解析器及其优势,我建议您使用其中之一。

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. 您可以解析RSS-FEED中的特殊字符。 Like in this question: JAVA: check a string if there is a special character in it 像这个问题一样: JAVA:检查字符串中是否包含特殊字符

and format them to valid string characters. 并将其格式化为有效的字符串字符。

如果您要在Java的String文字中使用" ,则必须使用反斜杠将其转义,例如

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM