My iphone app uses regular expressions (with NSRegularExpression) to perform calculations over a very large number of strings (in the 1000s). This of course takes a lot of time. What are some strategies to speed up the regular expressions? I looked into using blocks, but I don't think it will do any good -- they seem to mostly represent lambda functionality (i.e., equivalent to lisp) and are used on the Mac with multiple cores. Obviously, the current iPhone doesn't have multiple cores.
Here's my code:
NSString *replaceRegexPattern = @"([\\(|\\[].*?[\\)|\\]])|(^to )";
NSRegularExpression *replaceRegex = [[NSRegularExpression regularExpressionWithPattern:replaceRegexPattern
options:NSRegularExpressionCaseInsensitive
error:nil] retain];
NSArray *myArray = <some data>;
NSString *myString, *compareValue;
for (i = 0; i < [myArray count]; i++) {
myString = [myArray objectAtIndex:i];
compareValue = [replaceRegex stringByReplacingMatchesInString:myString
options:0
range:NSMakeRange(0, [myString length])
withTemplate:@""];
// do things with compareValue
}
To answer the question below, my goal in this code is to remove any text in my string which either is enclosed in parentheses, or which begins with "to ". Here are some examples:
- Hello (Goodbye) --> Hello
- Hello (Goodbye [n]) --> Hello
- To Say --> Say
- To Say (pf) --> Say