views:

835

answers:

2

I've taken the suggestion of some posts here that recommend regexkit lite with a problem I am having with trying to extract a particular URL from a string. The problem is that I'm very lost with the syntax of using it and hoping someone that has used it can give me a hand.

The string i'm trying to parse looks someting like this:

<a> blah blah blah <img src="http://www.something.com/1234.jpg"&gt;

All I want to extract is only the 1234. It would be after a "/" and before ".jpg"

How would I do that?

Thanks!

+1  A: 

Here's a recipe you should be able to adapt, from RegexKitLite's documentation:

NSString *searchString =@"http://johndoe:[email protected]:8080/private/mail/index.html";
NSString *regexString  = @"\\b(https?)://(?:(\\S+?)(?::(\\S+?))?@)?([a-zA-Z0-9\\-.]+)(?::(\\d+))?((?:/[a-zA-Z0-9\\-._?,'+\\&%$=~*!():@\\\\]*)+)?";

if([searchString isMatchedByRegex:regexString]) {
  NSString *protocolString = [searchString stringByMatching:regexString capture:1L];
  NSString *userString     = [searchString stringByMatching:regexString capture:2L];
  NSString *passwordString = [searchString stringByMatching:regexString capture:3L];
  NSString *hostString     = [searchString stringByMatching:regexString capture:4L];
  NSString *portString     = [searchString stringByMatching:regexString capture:5L];
  NSString *pathString     = [searchString stringByMatching:regexString capture:6L];

  NSMutableDictionary *urlDictionary = [NSMutableDictionary dictionary];

  if(protocolString) { [urlDictionary setObject:protocolString forKey:@"protocol"]; }
  if(userString)     { [urlDictionary setObject:userString     forKey:@"user"];     }
  if(passwordString) { [urlDictionary setObject:passwordString forKey:@"password"]; }
  if(hostString)     { [urlDictionary setObject:hostString     forKey:@"host"];     }
  if(portString)     { [urlDictionary setObject:portString     forKey:@"port"];     }
  if(pathString)     { [urlDictionary setObject:pathString     forKey:@"path"];     }

  NSLog(@"urlDictionary: %@", urlDictionary);
}
Dan Lorenc
+2  A: 

If you're trying to parse any string that looks like /1234.jpg, then regular expressions are a good way to go. If you need strings that look like that only in img tags, then use an HTML parser, not regex.

If it's the first case, this expression will match "/1234.jpg". You can get rid of the / and .jpg parts easily.

(/.+?\\.jpg)

The expression reads "look for any string starting with /, ending with .jpg and containing anything in between."

And using look-aheads and look-behinds, this one matches just "1234":

(?<=/).+?(?=\\.jpg)

This expression reads "look behind for /, match anything until .jpg is next."

Welbog
I tried NSString *regExString = @"(/.+?\\.jpg)";NSString *matchedString = [dataString stringByMatching:regExString capture:1L];I got everything BUT what I was looking for. Did I do something wrong?and got everything BUT the
Xcoder
Got it... Thanks!
Xcoder