Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

getting names subgroups

I am working with the new version of boost 1.42 and I want to use regex with named sub groups. Below an example.

std::string line("match this here FIELD=VALUE in the middle");
boost::regex rgx("FIELD=(?<VAL>\\w+)", boost::regex::perl );
boost::smatch thisMatch;
boost::regex_search( line, thisMatch, rgx );

Do you know how to get the content of the match ? The traditional way is

std::string result( mtch[1].first, mtch[1].second );

but i don't want to use this way.

I want to use the name of the subgroups as usual in Perl and in regex in general. I tried this, but it didn't work.

std::string result( mtch["VAL"].first, mtch["VAL"].second );

Do you know how to get the value using the name of the subgroup?

Thanks AFG

like image 555
Abruzzo Forte e Gentile Avatar asked Jul 10 '26 23:07

Abruzzo Forte e Gentile


2 Answers

AFAIK, there is no such option. See Understanding Marked Sub-Expressions and Captures and in particular the table on Perl and Boost.Regex equivalence. You will need to use the boost::match_results<IteratorType> to access any and all matches.

like image 188
dirkgently Avatar answered Jul 13 '26 16:07

dirkgently


I finally found what I want to achieve.

std::cout << mtch["VAL"] << std::endl;

I tried and it will work without any problems.

I think that is a feature available only since version 1.42 of boost, but I am not sure.

like image 30
Abruzzo Forte e Gentile Avatar answered Jul 13 '26 16:07

Abruzzo Forte e Gentile



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!