Roger Luo

超越梦想一起飞
  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

C++ Boost::Regex Usage

Posted on 2013-03-18 10:26  Roger Luo  阅读(296)  评论(0编辑  收藏  举报

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.