Here is the code
NSError *error = nil;
NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:@"[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*@(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+(?:[A-Z]{2}|com|org|net|edu|gov|mil|biz|info|mobi|name|aero|asia|jobs|museum)" options:NSRegularExpressionCaseInsensitive error:&error];
The code itself works but gives this warning:
"Unknown escape sequence \."
I also tried the options:
NSRegularExpressionCaseInsensitive|NSRegularExpressionAnchorsMatchLines
but still the error persists. Can anyone explain why this error comes up and how it can be removed.
If you write your regular expression as a string literal and it contains backslashes, you have to escape them using a second backslash because the backslash is also used for escaping some special characters (e.g.
\n,\tetc.) in strings literals.So if you want your regular expression to contain
\., you have to write it as\\.. Again, this only applies if you use string literals, not if you would load your regex from file.If you actually want to have a period without a backslash (which is effectively what you have now), remove the backslash to get rid of the warning.