I have a simple regex search and replace method. Everything works fine as expected, however when I was hammer testing yesterday the string I entered had "????" in it. this caused the regex to fail with the following error...
error NSError * domain: @"NSCocoaErrorDomain" - code: 2048 0x0fd3e970
upon further research I believe that it might be treating the question marks as a "trigraph". Chuck has a good explanation in this post.What does the \? (backslash question mark) escape sequence mean?
I tried to escape the sequence prior to creating the regex with this
string = [string stringByReplacingOccurrencesOfString:@"\?\?" withString:@"\?\\?"];
and it seem to stop the error but the search and replace no longer works. Here is the method I am using.
- (NSString *)searchAndReplaceText:(NSString *)searchString withText:(NSString *)replacementString inString:(NSString *)text {
NSRegularExpression *regex = [self regularExpressionWithString:searchString];
NSRange range = [regex rangeOfFirstMatchInString:text options:0 range:NSMakeRange(0, text.length)];
NSString *newText = [regex stringByReplacingMatchesInString:text options:0 range:range withTemplate:replacementString];
return newText;
}
- (NSRegularExpression *)regularExpressionWithString:(NSString *)string {
NSError *error = NULL;
NSString *pattern = [NSString stringWithFormat:@"\\b%@\\b", string];
NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:pattern options:NSRegularExpressionCaseInsensitive error:&error];
if (error)
NSLog(@"Couldn't create regex with given string and options");
return regex;
}
My questions are; is there a better way of escaping this sequence? Is this a case of trigraphs, or another possibility? Or is a there a way in code of ignoring trigraphs or turning this off?
Thanks