XCode:摆脱警告“未知转义序列\+”
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8233066/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
XCode: Getting rid of warning "Unknown escape sequence \+"
提问by toom
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?
对于转义序列,我收到诸如“未知转义序列 +”之类的编译器警告,这非常烦人,因为在我的情况下这并没有错。我怎样才能摆脱这个警告?
回答by zaph
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.
要\
在字符串中获取 a需要对其进行转义:\\
。因此\\\+
将第一个\
应用于转义第二个\
,第三个\
尝试转义加号+
,这是非法转义。