Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

XCode: Getting rid of warning "Unknown escape sequence \+"

In my code I make extensive use of regular expression, and my patterns look something like this

regexp = [[NSRegularExpression alloc] initWithPattern:@".*?\\\+.*?\\\+.*?\\\+.*?\\\+.*?\\\+.*?\\\+" options:0 error:nil];

For the escape sequences I get compiler warnings like "Unknown escape sequence +" which is extremely annoying because it is not wrong in my case. How can I get rid of this warning?

like image 578
toom Avatar asked Dec 27 '22 10:12

toom


1 Answers

You have to many back-slash characters, use:

regexp = [[NSRegularExpression alloc] initWithPattern:@".*?\\+.*?\\+.*?\\+.*?\\+.*?\\+.*?\\+" options:0 error:nil];

To get a \ in a string it needs to be escaped: \\. So \\\+ applies the first \ to escaping the second \ and the third \ tries to escape the plus sign + which is an illegal escape.

like image 95
zaph Avatar answered Feb 19 '23 15:02

zaph