Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Boost Regular Expression: Getting the Named Group

Tags:

c++

regex

boost

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"
}
like image 772
Ralph Avatar asked Jan 28 '26 17:01

Ralph


1 Answers

You can get it from match_result It is for xpressive, but the same should work for Boost.Regex

like image 69
Revolver_Ocelot Avatar answered Jan 31 '26 05:01

Revolver_Ocelot



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!