Xcode: getting rid of the "Unknown escape sequence \ +" warning

In my code, I make extensive use of regex and my patterns look something like this.

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

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

+4
source share
1 answer

You have many backslash characters, use:

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

To get the string \ in the string, it must be escaped: \\ . Thus, \\\+ applies the first \ to the screening of the second \ , and the third \ tries to avoid the plus sign + , which is an illegal escape.

+9
source

Source: https://habr.com/ru/post/1382567/


All Articles