Boost.Regex: Iterate over named captures/groups

Hi @all, if I have a regex (e.g.: "^Subject: (Re: |Fw: )*(?<subject>.*)") I know I can access them with what["subject"] but is there a way to get a list or whatever of named captures/groups? Maybe something like the following: foreach(const std::string &str, what.namedGroups()) { std::cout << "Group Name: " << what[str] << std::endl; } Thanks in advance Best Regards NoRulez

NoRulez
if I have a regex (e.g.: "^Subject: (Re: |Fw: )*(?<subject>.*)")
I know I can access them with what["subject"] but is there a way to get a list or whatever of named captures/groups?
What would you expect to see? You asked for a named group "subject", which matches zero or more characters; as such, it will *always* match (if the whole match succeeds) but it might be empty. And as for "(Re: |Fw: )*", it will always be submatch 1, and its contents will most likely be whatever it found on the last time through. You do have one good question here: I don't know how Boost.Regex would signal that "(Re: |Fw: )*" didn't match at all. (Perl would use undef to distinguish between that case and the empty string, but C++ doesn't have that option.) And it looks like the answer is (surprise!) in the documentation already: http://www.boost.org/doc/libs/1_48_0/libs/regex/doc/html/boost_regex/capture... (or: http://preview.tinyurl.com/846ls72 ) Hope this helps, t.

Hi,
You do have one good question here
My question was the following: Is there a way to get a list or whatever of named captures/groups? Here is an example: print_captures("a(?<val1>.*)", "abcbah"); The expected result should be (or something similar): Expression: "a(?<val1>.*)" Text: "abcbah" ** Match found ** Sub-Expressions: $0 = "abcbah" $1 = "bcbah" $+{val1} = "bcbah" Captures: $0 = { "abcbah" } $1 = { "bcbah" } $+{val1} = { "bcbah" } But I got: Expression: "a(?<val1>.*)" Text: "abcbah" ** Match found ** Sub-Expressions: $0 = "abcbah" $1 = "bcbah" Captures: $0 = { "abcbah" } $1 = { "bcbah" } Best Regards -----Ursprüngliche Nachricht----- Von: Anthony Foiani [mailto:tkil@scrye.com] Gesendet: Dienstag, 31. Jänner 2012 17:49 An: boost-users@lists.boost.org Cc: NoRulez Betreff: Re: [Boost-users] Boost.Regex: Iterate over named captures/groups NoRulez < mailto:norulez@me.com norulez@me.com> writes:
if I have a regex (e.g.: "^Subject: (Re: |Fw: )*(?<subject>.*)")
I know I can access them with what["subject"] but is there a way to get
a list or whatever of named captures/groups?
What would you expect to see? You asked for a named group "subject", which matches zero or more characters; as such, it will *always* match (if the whole match succeeds) but it might be empty. And as for "(Re: |Fw: )*", it will always be submatch 1, and its contents will most likely be whatever it found on the last time through. You do have one good question here: I don't know how Boost.Regex would signal that "(Re: |Fw: )*" didn't match at all. (Perl would use undef to distinguish between that case and the empty string, but C++ doesn't have that option.) And it looks like the answer is (surprise!) in the documentation already: <http://www.boost.org/doc/libs/1_48_0/libs/regex/doc/html/boost_regex/captur es.html#boost_regex.captures.unmatched_sub_expressions> http://www.boost.org/doc/libs/1_48_0/libs/regex/doc/html/boost_regex/capture s.html#boost_regex.captures.unmatched_sub_expressions (or: http://preview.tinyurl.com/846ls72 http://preview.tinyurl.com/846ls72 ) Hope this helps, t.

My question was the following: Is there a way to get a list or whatever of named captures/groups?
No sorry, not even Boost.Regexes private methods provide that functionality. If there's a convincing use case then I can add that support I guess, but I don't think this is something that even Perl provides? Regards, John.

Hi John,
I don't know if perl provides it, but PCRE based regex engines does (like Python or PHP)
It would be great if such feature could be implemented and/or access functions could be made public.
For regex_replace as an example the user then knows which sub expression could be used.
Best Regards
NoRulez
Am 01.02.2012 um 13:13 schrieb John Maddock
My question was the following: Is there a way to get a list or whatever of named captures/groups?
No sorry, not even Boost.Regexes private methods provide that functionality.
If there's a convincing use case then I can add that support I guess, but I don't think this is something that even Perl provides?
Regards, John. _______________________________________________ Boost-users mailing list Boost-users@lists.boost.org http://lists.boost.org/mailman/listinfo.cgi/boost-users
participants (4)
-
Anthony Foiani
-
John Maddock
-
NoRulez
-
norulez@me.com