|
Boost Users : |
From: Eric Niebler (eric_at_[hidden])
Date: 2008-07-14 12:10:47
Peter Boot wrote:
> Is there functionality included in boost::xpressive::regex_search(...)
> that would allow you to know which of an alternated group of expressions
> e.g. "aaa|bbb|ccc" matched a substring in the input ?
Just as with Perl and with Boost.Regex, you could create backreferences
and check to see which participated in the match:
"(aaa)|(bbb)|(ccc)"
Then pass a match_results<> object to regex_search and check
"what[N].matched" for N=1,2,3 (assuming your match_results<> object was
named "what").
If the alternates are all string literals, then you can use an xpressive
symbol table instead and discover which alternate matched more
efficiently. For that you'd initialize a std::map with your alternates ...
std::map<string,int> syms;
syms["aaa"] = 1;
syms["bbb"] = 2;
syms["ccc"] = 3;
int which = 0;
// Match a symbol, write the corresponding
// integer into "which"
sregex rx = (a1= syms)[ ref(which) = a1 ];
HTH,
-- Eric Niebler BoostPro Computing http://www.boostpro.com
Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net