C ++ 11正则表达式分组 [英] C++11 Regex Grouping

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

问题描述


可能重复:

与c ++ 11正则表达式无匹配


阅读这本涵盖C ++ 11的书,我到了有关正则表达式的章节。一切都对我很好,直到我得到关于分组的部分,并指向\1每当我需要回到一个组。我的代码编译完全正常:

  #include< iostream& 
#include< regex>
#include< string>
int main()
{
try
{


regex r1(<(。*)>。* ; / \\1>);
bool found = regex_match(string(< tag> value< / tag>),r1);
cout<< 字符串是否匹配:;
cout<< boolalpha<发现< '\\\
';
} catch(exception e)
{
cout<< e.what()< '\\\
';
}

return 0;然而,会发生什么是我得到一个分段故障(核心转储)消息。这个小片段直接从书中使用,所以我很怀疑这是错的。
注意:我使用-std = c ++ 0x编译。
注意:我在Ubuntu 12.04下使用Code :: Blocks



任何帮助都非常感谢!
谢谢!

解决方案

libstdc ++的regex的实现是HILARIOUSLY不完整。



http://gcc.gnu.org /onlinedocs/libstdc++/manual/status.html#status.iso.2011



向下转到第28节的正则表达式。



这是一个耻辱,因为g ++的工具链为C + +11否则非常坚固。



您可能需要使用boost或另一个regex库。或者改变clang(和clang的库)或MSVC(及其库)。


Possible Duplicate:
No matches with c++11 regex

I was reading this book that covers C++11 and I got to the chapter that talks about Regular Expressions. Everything was working fine for me until I got the part about grouping and referring to \1 whenever I needed to refer back to a group. My code compiles perfectly fine :

#include <iostream>
#include <regex>
#include <string>
int main()
{
     try
     {


           regex r1("<(.*)>.*</\\1>");
           bool found = regex_match(string("<tag>value</tag>"),r1);
           cout << "Does the string match:        ";
           cout << boolalpha << found << '\n';
     }catch( exception e)
     {
        cout << e.what() << '\n';
     }

     return 0;
}

What happens however is I get a segmentation fault (core dumped) message. This little snippet was used directly from the book so I highly doubt this is wrong. Note: I do compile using -std=c++0x. Note: I'm using Code::Blocks under Ubuntu 12.04

Any help would be highly appreciated! Thanks!

解决方案

libstdc++'s implementation of regex is HILARIOUSLY incomplete.

http://gcc.gnu.org/onlinedocs/libstdc++/manual/status.html#status.iso.2011

Head down to section 28, regular expressions. If you're using libstdc++ (which is almost certain as you're on ubuntu) then you cannot use regex yet.

It's a shame, because g++'s toolchain for C++11 is otherwise extremely solid.

You'll need to use boost for this probably, or another regex library. Or change over the clang (and clang's libraries) or to MSVC (and their libraries).

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

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