简体   繁体   中英

Boost Regular Expression: Getting the Named Group

How can I get the name of the group corresponding to the pattern match using Boost regular expressions?

The following will output the matched expression to the given pattern. But how can I get the corresponding named group?

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"
}

你可以从match_result获得它它适用于xpressive ,但同样适用于Boost.Regex

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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