0

I used this regex expression to search for img src in a string in one on my site. Now I wan't to use this expression to do the same thing in objective c. How can I do that using RegexKitLite? This is my expression

/<img.+src=[\'"]([^\'"]+)[\'"].*>/i

@Tim Pietzcker

Your code works great but for example if I try to search img in this string

<p><a href="http://www.nationalgeographic.it/popoli-culture/2011/08/03/news/per_la_tomba_del_boia-443612/?rssimage"> <img src="http://www.nationalgeographic.it/images/2011/07/29/115624013-20034abf-4d91-40fe-98ab-782f06a9854d.jpg" width="140" align="left" hspace="10"></a>Scoperta in America del Sud la sepoltura pre-incaica di un uomo circondato da coltelli cerimoniali che secondo gli archeologi eseguiva sacrifici umani</p>

I have this result in my array:

matchArray: (
    "<img src=\"http://www.nationalgeographic.it/images/2011/07/29/115624013-20034abf-4d91-40fe-98ab-782f06a9854d.jpg\" width=\"140\" align=\"left\" hspace=\"10\">"
)

How can I mod your regex to only get the content of src tag? thank you so much

1
  • I am pretty sure that apple are not accepting application that use RegexKitLite any more because it used a private library (libicu), you are supposed to use NSRegularExpression now, which uses the same libicu library. Commented Aug 4, 2011 at 10:17

1 Answer 1

1

The / delimiters are throwing you off. Also, you should at least use lazy quantifiers. Try this:

NSString *regexString = @"(?i)<img.+?src=['\"]([^'\"]+)['\"].*?>";

This breaks when filenames contain quotes, by the way. Could that be a problem for you?

A regex that's a bit safer (and that handles quotes well) would be

NSString *regexString = @"(?i)<img[^<>]+?src=(['\"])((?:(?!\\1).)+)\\1[^<>]*>";

However, now the matches filename will be in capture group 2, not 1, so you need to modify any code that uses the filename after the match.

Sign up to request clarification or add additional context in comments.

2 Comments

Thank you! Your code works but I only wan't the content of the src= tag! I added some info at my question! look
You need to access the second capturing group. I think you get this using the capture:2L parameter. Check page 14 in the RegexKitLite manual.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.