c++ regex匹配大括号内的内容

时间:2021-01-24 00:08:44

Suppose I would like to do extract the contents of matching curly braces using C++11 regex. So, for example, {foo} would match successfully and I could the use the match_result to extract the contents. It seems simple, but the following code does not quite do what I wish

假设我想使用c++ 11 regex提取匹配花括号的内容。例如,{foo}将成功匹配,我可以使用match_result来提取内容。这看起来很简单,但是下面的代码并不能完全实现我的愿望

std::string foo("{foo}");
std::regex r("\\{(.*)\\}");
std::smatch m;
bool result = regex_match(foo, m, r); // result returns true

cout << m[0] << endl; // prints: {foo}
cout << m[1] << endl; // prints: {foo} instead of just foo as I would expect

Now shouldn't m[1] return just foo without the braces given that it is the first capture group?

现在,m[1]不应该只返回foo,而不带括号,因为它是第一个捕获组吗?

EDIT: An essential piece of information for this question is that the compiler I'm using is GCC 4.6.3 (which currently is the latest repository version in Ubuntu 12.04 LTS). The answer identifies precisely how poor the support for regex in GCC is.

编辑:这个问题的一个重要信息是,我正在使用的编译器是GCC 4.6.3(目前是Ubuntu 12.04 LTS的最新版本)。答案准确地指出了在GCC中对regex的支持有多糟糕。

1 个解决方案

#1


3  

Your pattern is correct. Your code is largely correct with few missing 'std'' here and there (for 'cout', 'endl' and regex_match), or at least inconsistent (assuming you are 'using namespace std').

你的模式是正确的。您的代码在很大程度上是正确的,在这里和那里几乎没有丢失的“std”(对于“cout”、“endl”和regex_match),或者至少是不一致的(假设您使用的是“名称空间std”)。

Moreover, on Visual Studio 2012, your code output the expected result. I didn't try 2010, but I suspect it is running there as well (Microsoft incorporated TR1 back in 2010).

此外,在Visual Studio 2012上,您的代码将输出预期的结果。我没有尝试2010,但我怀疑它也在那里运行(微软在2010年合并了TR1)。

I suspect you are using gcc. As @Artyom pointed out, in gcc/libstdc++ isn't implemented. It compiles fine with no warnings but it gives the wrong results. Despite the common belief that gcc is superior than Microsoft in every area, this isn't the case in regular expression.

我怀疑你在使用gcc。正如@Artyom指出的,在gcc/libstdc++ +中没有实现。它在没有任何警告的情况下编译良好,但是会产生错误的结果。尽管人们普遍认为gcc在各个领域都优于微软,但在正则表达式中并非如此。

Find status of regex on gcc here: http://gcc.gnu.org/onlinedocs/libstdc++/manual/status.html#status.iso.200x

在这里找到regex的状态:http://gcc.gnu.org/onlinedocs/libstdc+ /manual/status.html#status.iso.200x

#1


3  

Your pattern is correct. Your code is largely correct with few missing 'std'' here and there (for 'cout', 'endl' and regex_match), or at least inconsistent (assuming you are 'using namespace std').

你的模式是正确的。您的代码在很大程度上是正确的,在这里和那里几乎没有丢失的“std”(对于“cout”、“endl”和regex_match),或者至少是不一致的(假设您使用的是“名称空间std”)。

Moreover, on Visual Studio 2012, your code output the expected result. I didn't try 2010, but I suspect it is running there as well (Microsoft incorporated TR1 back in 2010).

此外,在Visual Studio 2012上,您的代码将输出预期的结果。我没有尝试2010,但我怀疑它也在那里运行(微软在2010年合并了TR1)。

I suspect you are using gcc. As @Artyom pointed out, in gcc/libstdc++ isn't implemented. It compiles fine with no warnings but it gives the wrong results. Despite the common belief that gcc is superior than Microsoft in every area, this isn't the case in regular expression.

我怀疑你在使用gcc。正如@Artyom指出的,在gcc/libstdc++ +中没有实现。它在没有任何警告的情况下编译良好,但是会产生错误的结果。尽管人们普遍认为gcc在各个领域都优于微软,但在正则表达式中并非如此。

Find status of regex on gcc here: http://gcc.gnu.org/onlinedocs/libstdc++/manual/status.html#status.iso.200x

在这里找到regex的状态:http://gcc.gnu.org/onlinedocs/libstdc+ /manual/status.html#status.iso.200x