Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to iterate named groups in xpressive?

Say I have a sregex object like this one:

boost::xpressive::sregex::compile("(?P<firstword>\\w+) (?<secondword>\\w+)!");

I have not been able to find any reference in the xpressive documentation regarding this, despite xpressive supporting named groups just fine.

I know that it is possible to iterate through groups is just fine, but how would I access the groupname (if the group has a name at all)?

So, How would I iterate through the named groups?

like image 247
hiobs Avatar asked Oct 09 '22 04:10

hiobs


1 Answers

Supposing that we have the whole regular expression you are working on, iIt seems to my point of view that you are trying to create a regular expression that match both of the the named capture, so it is useless to try to iterate over named capture.

You simply have to try something like that.

std::string str("foo bar");
sregex rx = sregex::compile("(?P<firstword>\\w+) (?<secondword>\\w+)!");
smatch what;
if(regex_search(str, what, rx))
{
    std::cout << "char = " << what["firstword"]  << what["secondword"] std::endl;
}

If the regular expression is part of a more complex pattern why not use the static named capture : http://www.boost.org/doc/libs/1_41_0/doc/html/xpressive/user_s_guide.html#boost_xpressive.user_s_guide.named_captures.static_named_captures

like image 105
VGE Avatar answered Oct 17 '22 14:10

VGE