C语言 警告:未知的转义序列 '\
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/42022735/
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
warning: unknown escape sequence '\
提问by Santhosh Pai
I'm trying to run a regex through a system command in the code, I have gone through the threads in StackOverflow on similar warnings but I couldn't understand on how to fix the below warnings, it seems to come only for the closed brackets on doing \\}. The warnings seem to disappear but not able to get the exact output in the redirected file.
我正在尝试通过代码中的系统命令运行正则表达式,我已经浏览了 StackOverflow 中类似警告的线程,但我无法理解如何修复以下警告,它似乎只适用于封闭括号在做\\}。警告似乎消失了,但无法在重定向文件中获得确切的输出。
#include<stdio.h>
int main(){
FILE *in;
char buff[512];
if(system("grep -o '[0-9]\{1,3\}\.[0-9]\{1,3\}\.[0-9]\{1,3\}\.[0-9]\{1,3\}' /home/santosh/Test/text >t2.txt") < 0){
printf("system failed:");
exit(1);
}
}
Warnings:
警告:
dup.c:9:11: warning: unknown escape sequence '\}'
dup.c:9:11: warning: unknown escape sequence '\}'
dup.c:9:11: warning: unknown escape sequence '\}'
dup.c:9:11: warning: unknown escape sequence '\}'
dup.c: In function 'main':
回答by Jabberwocky
In C string literals the \has a special meaning, it's for representing characters such as line endings \n. If you want to put a \in a string, you need to use \\.
在 C 字符串文字中\具有特殊含义,它用于表示诸如行尾之类的字符\n。如果要将 a\放入字符串中,则需要使用\\.
For example
例如
"\Hello\Test"
will actually result in "\Hello\Test".
实际上会导致“\Hello\Test”。
So your regexp needs to be written as:
所以你的正则表达式需要写成:
"[0-9]\{1,3\}\\.[0-9]\{1,3\}\\.[0-9]\{1,3\}\\.[0-9]\{1,3\}"
instead of:
代替:
"[0-9]\{1,3\}\.[0-9]\{1,3\}\.[0-9]\{1,3\}\.[0-9]\{1,3\}"
Sure this is painful because \is used as escape character for the regexp and again as escape character for the string literal.
当然这很痛苦,因为它\被用作正则表达式的转义字符,并再次用作字符串文字的转义字符。
So basically: when you want to put a \you need to write \\.
所以基本上:当你想把 a\你需要写\\.

