[英]Dot in regular expression (regex)
I am using slre ( https://code.google.com/p/slre/ ) for providing a regex library for ac program. 我正在使用slre( https://code.google.com/p/slre/ )为ac程序提供正则表达式库。
I want to match an IP address with following pattern: "^(([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])\\.){3}([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])$"
我想用以下模式匹配IP地址:
"^(([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])\\.){3}([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])$"
I get following compile error: Warning: unknown excape sequence '\\.' 我收到以下编译错误:警告:未知的基音序列'\\。 I also tried it with '\\\\.'
我也尝试了'\\\\'。 --> the compile error is gone, but it's still saying it doesn't match.
->编译错误消失了,但是仍然说它不匹配。
if (!slre_compile(&slre, settings[i].regex)) {
printf("Error compiling RE: %s\n", slre.err_str);
}
else if (!slre_match(&slre, settings[i].value, strlen(settings[i].value), captures)) {
printf("\nSetting '%s' does not match the regular expression!", settings[i].internName);
}
settings[i].regex is a char* with the regular expression I mentioned above settings[i].value is a char* the string I am trying to match is 8.8.8.8 settings [i] .regex是我上面提到的正则表达式的char *设置[i] .value是char *我要匹配的字符串是8.8.8.8
Is there any other way to check for a dot? 还有其他检查点的方法吗?
Try [.]
尝试
[.]
Dot isn't special inside character class. 点在字符类中并不特殊。
The C compiler is seeing your backslash as an attempt to escape a character in C, in the same way that \\n
becomes a newline. C编译器将反斜杠视为企图在C中转义字符的尝试,就像
\\n
成为换行符一样。 You need to use a double-backslash: 您需要使用双反斜杠:
\\.
The C compiler will turn that into a single backslash and pass that to the regex library. C编译器会将其转换为单个反斜杠,并将其传递给regex库。
That's the source of the compiler warning - if it's still not matching after you add the extra backslash then you have a different problem as well. 这就是编译器警告的来源-如果在添加额外的反斜杠后仍不匹配,那么您也会遇到其他问题。
According to http://derekslager.com/blog/posts/2007/09/a-better-dotnet-regular-expression-tester.ashx your regex does match 8.8.8.8
, so the problem isn't with the regex itself. 根据http://derekslager.com/blog/posts/2007/09/a-better-dotnet-regular-expression-tester.ashx,您的正则表达式确实与
8.8.8.8
匹配,因此问题不在于正则表达式本身。
Your question is about C, but if you can compile with C++11 you can have a look to literal raw string 您的问题是关于C的,但是如果可以使用C ++ 11进行编译,则可以查看原义的原始字符串
http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2007/n2442.htm http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2007/n2442.htm
std::string literal_string = R"literal string\. \n";
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.