这是C ++ 11正则表达式错误我还是编译器? [英] Is this C++11 regex error me or the compiler?

查看:137
本文介绍了这是C ++ 11正则表达式错误我还是编译器?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好吧,这不是原来的程序我有这个问题,但我复制它在一个更小的一个。很简单的问题。



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;
}

使用此命令编译成功,没有错误消息:

  $ g ++ -std = c ++ 0x main.cpp 

g ++ -v 的最后一行是:

  gcc version 4.6.1(Ubuntu / Linaro 4.6.1-9ubuntu3)

当我尝试运行它时的结果:

  $ ./a.out 
S 。
。作品。
。+工作。
抛出'std :: regex_error'实例后终止调用
what():regex_error
中止

如果我将r4更改为 \\s \\w [az] 。这是编译器的问题吗?我可能可以相信C ++ 11的正则表达式引擎有不同的方式来说空格或字字符,但方括号不工作是一个伸展。



Joachim Pileborg提供了一个部分解决方案,使用一个额外的 regex_constants 参数来启用支持方括号的语法,但不是 basic 扩展 awk ,也不是 ECMAScript 反斜杠转义的术语如 \\s \\w \\t



编辑2:

使用原始字符串( R(\w)而不是\\w )似乎也不工作。

解决方案

更新:< regex> 现在在GCC 4.9.0中实现和发布






旧答案: / p>

ECMAScript语法接受 [0-9] \s \w 等,请参阅 ECMA-262(15.10)。下面是一个使用默认情况下也使用ECMAScript语法的 boost :: regex 的示例:

  #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;
}

它的工作原理:

  $ g ++ -std = c ++ 0x * .cc -lboost_regex&& ./a.out 1 

根据C ++ 11标准(28.8.2)<$默认情况下,c $ c> 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.\n");
    regex r2(".");
    printf(". works.\n");
    regex r3(".+");
    printf(".+ works.\n");
    regex r4("[0-9]");
    printf("[0-9] works.\n");
    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).

这篇关于这是C ++ 11正则表达式错误我还是编译器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆