提升正则表达式:获取命名组

时间:2015-12-20 16:27:50

标签: c++ regex boost

如何使用Boost正则表达式获取与模式匹配相对应的组名?

以下内容将匹配的表达式输出到给定的模式。但是如何获得相应的命名组呢?

boost::regex pattern("(?<alpha>[0-9]*\\.?[0-9]+)|(?<beta>[a-zA-Z_]+)");

string s = "67.2 x  7 I am";

string::const_iterator start = s.begin();
string::const_iterator end   = s.end();
boost::sregex_token_iterator i(start, end, pattern);
boost::sregex_token_iterator j;

for ( ;i != j; ++i)
{
    cout << *i << endl;
        // '67.2' and '7' belongs to "alpha"
        // 'x', 'I', 'am' belongs to "beta"
}

1 个答案:

答案 0 :(得分:2)

You can get it from match_result适用于 xpressive ,但同样适用于Boost.Regex