Skip to content

Instantly share code, notes, and snippets.

@marcuswestin
Created October 1, 2012 17:02
Show Gist options
  • Save marcuswestin/3813026 to your computer and use it in GitHub Desktop.
Save marcuswestin/3813026 to your computer and use it in GitHub Desktop.
Code to match parts of a url using NSPredicates
NSURL* url = [NSURL URLWithString:@"http://www.google.com/path/to/foo?aParam=value"];
url.absoluteString;
url.host;
url.scheme;
url.path;
NSArray* arr = [NSArray arrayWithObjects:url, [NSURL URLWithString:@"https://marcuswest.in/teachers"], nil];
NSLog(@"scheme %@", [arr filteredArrayUsingPredicate:[NSPredicate predicateWithFormat:@"scheme MATCHES 'http'"]]);
NSLog(@"host %@", [arr filteredArrayUsingPredicate:[NSPredicate predicateWithFormat:@"host MATCHES 'www.google.com'"]]);
NSLog(@"host 2 %@", [arr filteredArrayUsingPredicate:[NSPredicate predicateWithFormat:@"host MATCHES '^www.google.*'"]]);
NSLog(@"path %@", [arr filteredArrayUsingPredicate:[NSPredicate predicateWithFormat:@"path MATCHES '^www.google.*'"]]);
NSLog(@"absolute %@", [arr filteredArrayUsingPredicate:[NSPredicate predicateWithFormat:@"absoluteString MATCHES '^http.*path.*'"]]);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment