简体   繁体   English

C ++正则表达式字符串捕获

[英]C++ regex string capture

Tring to get C++ regex string capture to work. Tring让C ++正则表达式字符串捕获工作。 I have tried all four combinations of Windows vs. Linux, Boost vs. native C++ 0x11. 我已经尝试了Windows与Linux的所有四种组合,Boost与本机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). 最接近的是带有Boost(1.51.0)的g ++(4.7)。 There, the first cout outputs the expected abhelloworld. 在那里,第一个cout输出预期的abhelloworld. but nothing from the second cout. 但第二个cout没什么。

g++ 4.7 with -std=gnu++11 and <regex> instead of <boost/regex.hpp> produces no output. g ++ 4.7 with -std = gnu ++ 11 and <regex>而不是<boost/regex.hpp>产生输出。

Visual Studio 2012 using native <regex> yields an exception regarding incompatible string iterators. 使用本机<regex> Visual Studio 2012会产生关于不兼容的字符串迭代器的异常。

Visual Studio 2008 with Boost 1.51.0 and <boost/regex.hpp> yields an exception regarding "Standard C++ Libraries Invalid argument". 带有Boost 1.51.0和<boost/regex.hpp> Visual Studio 2008会产生关于“标准C ++库无效参数”的异常。

Are these bugs in C++ regex, or am I doing something wrong? 这些错误是在C ++正则表达式中,还是我做错了什么?

Are these bugs in C++ regex, or am I doing something wrong? 这些错误是在C ++正则表达式中,还是我做错了什么?

gcc doesn't support <regex> as noted in the other answer. 如其他答案所述,gcc不支持<regex> 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. 您的原始示例进行编译,因为regex_search采用临时对象可以绑定的const引用,但是, smatch仅将迭代器存储到不再存在的临时对象中。 The solution is to not pass temporaries. 解决方案是不通过临时工。

If you look in the C++ standard at [§ 28.11.3/5], you will find the following: 如果你在[§28.11.3/ 5]中查看C ++标准,你会发现以下内容:

Returns: The result of regex_search(s.begin(), s.end(), m, e, flags). 返回: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 . 这意味着在内部,只使用传入的字符串的迭代器 ,因此如果传入临时对象,将使用无效的迭代器,并且不存储实际的临时对象。

GCC doesn't support <regex> yet. GCC还不支持<regex> Refer to the Manual 请参阅手册

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM