Xcoder
Xcoder

Reputation: 1031

Regexkit lite and iPhone parsing

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">

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

How would I do that?

Thanks!

Upvotes: 2

Views: 2175

Answers (2)

Welbog
Welbog

Reputation: 60398

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."

Upvotes: 2

Dan Lorenc
Dan Lorenc

Reputation: 5394

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);
}

Upvotes: 1

Related Questions