这是 C++11 正则表达式错误我还是编译器?
好的,这不是我遇到这个问题的原始程序,但我将它复制到一个更小的程序中.很简单的问题.
main.cpp:
#include #include <正则表达式>使用命名空间标准;int main(){正则表达式 r1("S");printf("S 有效.
");正则表达式 r2(".");printf(".有效.
");正则表达式 r3(".+");printf(".+ 有效.
");正则表达式 r4("[0-9]");printf("[0-9] 有效.
");返回0;}
用这个命令编译成功,没有错误提示:
$ g++ -std=c++0x main.cpp
顺便说一下,g++ -v
的最后一行是:
gcc 版本 4.6.1 (Ubuntu/Linaro 4.6.1-9ubuntu3)
当我尝试运行它时的结果:
$ ./a.outS作品..作品..+ 作品.抛出std::regex_error"实例后调用终止what(): regex_error中止
如果我将 r4 更改为 \s
、\w
或 [a-z]
,也会发生同样的情况.这是编译器的问题吗?我可能会相信 C++11 的正则表达式引擎有不同的说法空白"或单词字符",但方括号不起作用是一种延伸.是否已在 4.6.2 中修复?
Joachim Pileborg 提供了一个部分解决方案,使用额外的 regex_constants
参数来启用支持方括号的语法,但既没有 basic
,extended
、awk
和 ECMAScript
似乎都支持反斜杠转义的术语,例如 \s
、\w
,或 \t
.
编辑 2:
使用原始字符串(R"(w)"
而不是 "\w"
)似乎也不起作用.
更新:
现已在 GCC 4.9.0 中实现和发布
旧答案:
ECMAScript 语法接受 [0-9]
、s
、w
等,参见 ECMA-262 (15.10).下面是一个带有 boost::regex
的例子,它默认也使用 ECMAScript 语法:
#include int main(int argc, char* argv[]) {使用命名空间提升;正则表达式 e("[0-9]");返回 argc >1 ?!regex_match(argv[1], e) : 2;}
它有效:
$ g++ -std=c++0x *.cc -lboost_regex &&./a.out 1
根据 C++11 标准 (28.8.2) basic_regex()
默认使用 regex_constants::ECMAScript
标志,因此它必须理解此语法.><块引用>
这个 C++11 正则表达式错误是我还是编译器?
gcc-4.6.1 不支持 c++11 正则表达式 (28.13).
OK, this isn't the original program I had this problem in, but I duplicated it in a much smaller one. Very simple problem.
main.cpp:
#include <iostream>
#include <regex>
using namespace std;
int main()
{
regex r1("S");
printf("S works.
");
regex r2(".");
printf(". works.
");
regex r3(".+");
printf(".+ works.
");
regex r4("[0-9]");
printf("[0-9] works.
");
return 0;
}
Compiled successfully with this command, no error messages:
$ g++ -std=c++0x main.cpp
The last line of g++ -v
, by the way, is:
gcc version 4.6.1 (Ubuntu/Linaro 4.6.1-9ubuntu3)
And the result when I try to run it:
$ ./a.out
S works.
. works.
.+ works.
terminate called after throwing an instance of 'std::regex_error'
what(): regex_error
Aborted
It happens the same way if I change r4 to \s
, \w
, or [a-z]
. Is this a problem with the compiler? I might be able to believe that C++11's regex engine has different ways of saying "whitespace" or "word character," but square brackets not working is a stretch. Is it something that's been fixed in 4.6.2?
EDIT:
Joachim Pileborg has supplied a partial solution, using an extra regex_constants
parameter to enable a syntax that supports square brackets, but neither basic
, extended
, awk
, nor ECMAScript
seem to support backslash-escaped terms like \s
, \w
, or \t
.
EDIT 2:
Using raw strings (R"(w)"
instead of "\w"
) doesn't seem to work either.
Update: <regex>
is now implemented and released in GCC 4.9.0
Old answer:
ECMAScript syntax accepts [0-9]
, s
, w
, etc, see ECMA-262 (15.10). Here's an example with boost::regex
that also uses the ECMAScript syntax by default:
#include <boost/regex.hpp>
int main(int argc, char* argv[]) {
using namespace boost;
regex e("[0-9]");
return argc > 1 ? !regex_match(argv[1], e) : 2;
}
It works:
$ g++ -std=c++0x *.cc -lboost_regex && ./a.out 1
According to the C++11 standard (28.8.2) basic_regex()
uses regex_constants::ECMAScript
flag by default so it must understand this syntax.
Is this C++11 regex error me or the compiler?
gcc-4.6.1 doesn't support c++11 regular expressions (28.13).
相关文章