Skip to content

Instantly share code, notes, and snippets.

@isutton
Created July 9, 2012 08:51
Show Gist options
  • Save isutton/3075205 to your computer and use it in GitHub Desktop.
Save isutton/3075205 to your computer and use it in GitHub Desktop.
Regular expressions in Objective-C
#import <Foundation/Foundation.h>
int main(int argc, const char * argv[])
{
@autoreleasepool {
NSString *key = @"UINavigationBarTintColor";
NSString *pattern = @"(Color|Font|Height|Width|Size|Origin|Frame)$";
NSError *error = nil;
NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:pattern options:NSRegularExpressionCaseInsensitive error:&error];
if (regex) {
// Regular expression pattern is valid, let's check if we can find a match in the string.
NSTextCheckingResult *match = [regex firstMatchInString:key options:0 range:NSMakeRange(0, [key length])];
NSString *matchString = [key substringWithRange:match.range];
NSLog(@"matchString: %@", matchString);
}
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment