C++ Boost::Regex Usage

In Boost library, there are totally three libraries to support regex parser, regex, xpressive and ..

the xpressive library is static compile, that means it will cause a lot of time to build your project even if you just change small piece of code.

Regex usage

static boost::regex rex("<a[^>]*?href=\"(.[^>]*)(?=\")", boost::regex::icase);

boost::cmatch what;
boost::match_flag_type flags = boost::match_default;

string ss(psz);
boost::sregex_iterator itBgn(ss.begin(), ss.end(), rex);
boost::sregex_iterator itEnd;
for_each(itBgn, itEnd, [&urls](const boost::match_results<std::string::const_iterator>& what){
//cout<<what.str()<<endl;// 0 for whole match 1 for the first group
if (what.size() - 1 > 0)
urls.push_back(what[1].str());
});

the first match group is start index is 1. 0 is for the whole match for the regex syntax.

原文地址:https://www.cnblogs.com/rogerroddick/p/2965508.html