5

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?

hiobs
  • 618
  • 1
  • 6
  • 20
  • 2
    Duplicate: http://stackoverflow.com/questions/2718607/cboostregex-iterate-over-the-submatches – Lightness Races in Orbit Dec 27 '11 at 16:20
  • 2
    How is this a duplicate? The linked answer does **not** answer it, as it only quotes code directly from the header (apart from the fact that using that code would be a mere hack), it doesn't even feature an example. Why am I even trying to explain? Someone is going to tell me "that's totally a duplicate etc etc etc suck it up". SO is truly losing usefulness. And that's still put nicely, like a gentleman. – hiobs Dec 28 '11 at 05:35
  • 1
    Duplicates are not about answers; they are about questions. Your little rant doesn't change that – Lightness Races in Orbit Dec 28 '11 at 14:02

1 Answers1

4

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

VGE
  • 4,171
  • 18
  • 17
  • Unfortunately it appears that using `operator[]` is currently the only way to access named groups. I wish xpressive would make the groupname accessible via member functions... – hiobs Jan 03 '12 at 09:58