C ++则字符串捕获 [英] C++ regex string capture

查看:285
本文介绍了C ++则字符串捕获的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

特林得到C ++字符串的正则表达式捕捉工作。我已经试过的Windows Linux的对比四种组合,提升对本地C ++为0x11。样品code是:

 的#include<串GT;
#包括LT&;&iostream的GT;
#包括LT&;升压/ regex.hpp>
//#包括LT&;&正则表达式GT;使用命名空间std;
使用名字空间boost;INT主(INT ARGC,字符** argv的)
{
    SMATCH SM1;
    regex_search(字符串(abhelloworld.jpg),SM1,正则表达式((*),JPG。));
    COUT<< SM1 [1];&下; ENDL;
    SMATCH SM2;
    regex_search(字符串(hell.g),SM2,正则表达式((*)G));
    COUT<< SM2 [1];&下; ENDL;
}

这工作最接近的是G ++(4.7)与加速(1.51.0)。在那里,第一COUT输出预期的 abhelloworld。,但没有从第二COUT。

G ++ 4.7与-std = GNU + 11 <正则表达式> 而不是<升压/ regex.hpp> 不产生输出。

使用本机的Visual Studio 2012 <正则表达式的方式> 就产生不兼容的字符串迭代器异常

Visual Studio 2008中使用升压1.51.0和<升压/ regex.hpp方式> 就产生标准C ++库无效参数异常

难道这些漏洞在C ++中的正则表达式,还是我做错了什么?


解决方案

  

难道这些漏洞在C ++中的正则表达式,还是我做错了什么?


GCC不支持<正则表达式> 在对方回答说。至于其他问题,你的问题是你逝去的临时字符串对象。更改code以下内容:

  SMATCH SM1;
字符串S1(abhelloworld.jpg);
regex_search(S1,SM1,正则表达式()(*)JPG。);
COUT<< SM1 [1];&下; ENDL;
SMATCH SM2;
字符串s2(hell.g);
regex_search(S2,SM2,正则表达式()(*)G);
COUT<< SM2 [1];&下; ENDL;

您最初的例子编译,因为 regex_search 需要哪些临时对象可以绑定到,但是, SMATCH 只是一个const引用商店迭代器不再存在你的临时对象。该解决方案是无法通过临时

如果您在C ++标准在看[第28.11.3 / 5],你会发现以下内容:


  

返回:regex_search的结果(s.begin(),s.end(),M,E,标志)


这意味着在内部,只有的的迭代器的给你的字符串传递使用,因此,如果你在一个临时通行证,迭代器的临时对象将用于哪些是无效的和实际的临时本身的不保存

Tring to get C++ regex string capture to work. I have tried all four combinations of Windows vs. Linux, Boost vs. native C++ 0x11. The sample code is:

#include <string>
#include <iostream>
#include <boost/regex.hpp>
//#include <regex>

using namespace std;
using namespace boost;

int main(int argc, char** argv)
{
    smatch sm1;
    regex_search(string("abhelloworld.jpg"), sm1, regex("(.*)jpg"));
    cout << sm1[1] << endl;
    smatch sm2;
    regex_search(string("hell.g"), sm2, regex("(.*)g"));
    cout << sm2[1] << endl;
}

The closest that works is g++ (4.7) with Boost (1.51.0). There, the first cout outputs the expected abhelloworld. but nothing from the second cout.

g++ 4.7 with -std=gnu++11 and <regex> instead of <boost/regex.hpp> produces no output.

Visual Studio 2012 using native <regex> yields an exception regarding incompatible string iterators.

Visual Studio 2008 with Boost 1.51.0 and <boost/regex.hpp> yields an exception regarding "Standard C++ Libraries Invalid argument".

Are these bugs in C++ regex, or am I doing something wrong?

解决方案

Are these bugs in C++ regex, or am I doing something wrong?

gcc doesn't support <regex> as noted in the other answer. As for the other problems, your problem is you are passing temporary string objects. Change your code to the following:

smatch sm1;
string s1("abhelloworld.jpg");
regex_search(s1, sm1, regex("(.*)jpg"));
cout << sm1[1] << endl;
smatch sm2;
string s2("hell.g");
regex_search(s2, sm2, regex("(.*)g"));
cout << sm2[1] << endl;

Your original example compiles because regex_search takes a const reference which temporary objects can bind to, however, smatch only stores iterators into your temporary object which no longer exists. The solution is to not pass temporaries.

If you look in the C++ standard at [§ 28.11.3/5], you will find the following:

Returns: The result of regex_search(s.begin(), s.end(), m, e, flags).

What this means is that internally, only iterators to your passed in string are used, so if you pass in a temporary, iterators to that temporary object will be used which are invalid and the actual temporary itself is not stored.

这篇关于C ++则字符串捕获的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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