0

I have this HTML string that looks like this:

<html>
  <head>
    <meta name="viewport" content="width=device-width; initial-scale=1.0; maximum-scale=1.0; user-scalable=0;">
  </head>
  <body style="font-family: Arial, Helvetica, sans-serif; font-size: 20px; direction: rtl; text-align: rtl; word-wrap: break-word">abcdefghijklmnopqrstuvwxyz
    <img src="/Users/user/Library/Application Support/iPhone Simulator/7.0.3-64/Applications/7D48D94E-2FE5-4F66-AE29-B7E2EB30870B/MyApp.app/image003.png">&nbsp;
    <img src="/Users/user/Library/Application Support/iPhone Simulator/7.0.3-64/Applications/7D48D94E-2FE5-4F66-AE29-B7E2EB30870B/MyApp.app/image003.png">&nbsp;
    <img src="/Users/user/Library/Application Support/iPhone Simulator/7.0.3-64/Applications/7D48D94E-2FE5-4F66-AE29-B7E2EB30870B/MyApp.app/image003.png">&nbsp;
    <img src="/Users/user/Library/Application Support/iPhone Simulator/7.0.3-64/Applications/7D48D94E-2FE5-4F66-AE29-B7E2EB30870B/MyApp.app/image003.png">&nbsp;
  </body>
</html>

I'm building this string in the code by button click's and present it on a UIWebView. My problem is that I want to remove the last image from this string, but I don't know how. I think that the best way is to use Regular expression, but I don't know how to use it. Also I don't really know if it's possible, because I don't know what will be the last string, it could be just a char or it could be an image.

Someone have any idea?

Thanks in advance!

Jatin
  • 3,065
  • 6
  • 28
  • 42
ytpm
  • 4,962
  • 6
  • 56
  • 113
  • So you want to remove the last `` tag or the img src..? – Drewness Feb 26 '14 at 21:00
  • yeah, but just the last one, without any thing else. The last tag. – ytpm Feb 26 '14 at 21:12
  • I don't think this is possible via pure RegEx since it doesn't match in reverse. You could *use* RegEx to store an array of substrings matching: `]+>` then simply retrieve the last match. – tenub Feb 26 '14 at 21:26
  • Relevant: http://stackoverflow.com/questions/1732348/regex-match-open-tags-except-xhtml-self-contained-tags – xdumaine Feb 26 '14 at 21:35

1 Answers1

1

You could try something along the lines of:

<img.*?>(?=[^<]*</body>)

That would match the last <img> tag based on it being followed by the </body> tag. As others have pointed out though, parsing (x)html with regex is shady at best. Add some spaces into the </body> tag or a number of other things that would still be valid/correct html and the regex will fail.

A better solution would be to traverse the DOM and doing it that way.

Community
  • 1
  • 1
rvalvik
  • 1,559
  • 11
  • 15
  • Thanks for your answer man, I really don't know nothing about RegEx, I tried something but It's just removing all the and tags and leaving the tags. Can you please be more specific? – ytpm Feb 26 '14 at 23:49