0

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!

Isuru
  • 3,818
  • 13
  • 49
  • 64

3 Answers3

1

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.

Community
  • 1
  • 1
1

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.

Community
  • 1
  • 1
Jan Koester
  • 1,178
  • 12
  • 26
1

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 ";
Hurda
  • 4,647
  • 8
  • 35
  • 49
  • Thanks but I can't escape every feed by writing escapes. I need to do it programmitically. – Isuru Feb 23 '12 at 09:52
  • 1
    You don't have to escape feeds - you only nead to escape " in Strin LITERALS - what's your feed? How do you get the input? – Hurda Feb 23 '12 at 14:54