Introduction To Regular Expressions In Objective-C: Search String For Email Addresses
January 14, 2013
Regular expressions (aka regex) are extremely powerful for string matching, if you are not familiar with how to use them, I highly recommend you take some time to learn more.
To give you a taste, below is a block of code that uses the NSRegularExpression and NSTextCheckingResult classes to create a compiled regular expression and extract content from the same.
The example will search for all email addresses numbers in the following string:
NSString *str = @"Sed ut perspiciatis, MobileDeveloperTips@fubar.net unde omnis iste natus error youremail@xyz123abc.com sit voluptatem accusantium doloremque laudantium, totam rem aperiam eaque ipsa, quae ab illo inventore veritatis et quasi architecto beatae vitae dicta sunt, anotherEmailAddress@SomeServer.info explicabo."; |
The output we are after:
MobileDeveloperTips@fubar.net
youremail@xyz123abc.com
anotherEmailAddress@SomeServer.info
Here is the code to create the regex and parse the results:
NSError *error = nil; // String to search NSString *str = @"Sed ut perspiciatis, MobileDeveloperTips@fubar.net unde omnis iste natus error youremail@xyz123abc.com sit voluptatem accusantium doloremque laudantium, totam rem aperiam eaque ipsa, quae ab illo inventore veritatis et quasi architecto beatae vitae dicta sunt, anotherEmailAddress@SomeServer.info explicabo."; // Regular expression to parse email NSString *regexAsString = @"([a-zA-Z0-9_\\-\\.\\+])+\\@([a-zA-Z0-9_\\-\\.])+\\.([A-Za-z]+)"; // Create instance of NSRegularExpression // which is a compiled regular expression pattern NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:regexAsString options:0 error:&error]; // If no errors if (!error) { // Array of matches of regex in string NSArray *results = [regex matchesInString:str options:0 range:NSMakeRange(0, str.length)]; // View results to see range and length of matches NSLog(@"results: %@", results); int count = 1; // For each item found... for (NSTextCheckingResult *entry in results) { // Get email from the str using range NSString *text = [str substringWithRange:entry.range]; NSLog(@"Result %d: - %@", count++, text); } } else NSLog(@"Invalid epxression pattern: %@", error); |
The complete output is below:
results: (
"<NSExtendedRegularExpressionCheckingResult: 0x845c5d0>{21, 29}{<NSRegularExpression: 0x845b400> ([a-zA-Z0-9_\\-\\.\\+])+\\@([a-zA-Z0-9_\\-\\.])+\\.([A-Za-z]+) 0x0}",
"<NSExtendedRegularExpressionCheckingResult: 0x845c810>{79, 23}{<NSRegularExpression: 0x845b400> ([a-zA-Z0-9_\\-\\.\\+])+\\@([a-zA-Z0-9_\\-\\.])+\\.([A-Za-z]+) 0x0}",
"<NSExtendedRegularExpressionCheckingResult: 0x845c6c0>{261, 35}{<NSRegularExpression: 0x845b400> ([a-zA-Z0-9_\\-\\.\\+])+\\@([a-zA-Z0-9_\\-\\.])+\\.([A-Za-z]+) 0x0}"
)
2013-01-13 22:32:11.035 Sandbox[4492:c07] Result 1: - MobileDeveloperTips@fubar.net
2013-01-13 22:32:11.036 Sandbox[4492:c07] Result 2: - youremail@xyz123abc.com
2013-01-13 22:32:11.037 Sandbox[4492:c07] Result 3: - anotherEmailAddress@SomeServer.info |
Notice the range specified in the results, for example, {21, 29}, this is the location of the first match, position 21, the match is 29 characters long (MobileDeveloperTips@fubar.net).
Pretty slick isn’t it.



