Reputation: 3042
The answer to this may be a simple no, but here goes...
I'm currently using the boost function regex_match to evaluate a string against a regex value.
Instead of just returning T/F, is there a way to find out which element of multiple joined statements evaluated to true?
For example:
^a$|^z$|^p$
a --> 0
z --> 1
f --> -1
Upvotes: 0
Views: 124
Reputation: 23148
Enclose them in capturing parentheses, then test which sub-expression matched.
(^a$)|(^z$)|(^p$)
match_results m;
regex_match(..., m);
a -> m[1].matched
z -> m[2].matched
p -> m[3].matched
Update:
You might be able to improve on it by making a single capture group and testing the result, e.g.:
^([azp])$
...
if ('a' == m[0][0]) ...
Either method is almost certainly faster than calling regex_match
three times, though to be sure you just have to test it. Unless you're doing this really often, the difference is not worth worrying about.
Obviously, make sure that you're only setting up the regex once, not each time you need it.
If need it to be really, really fast you probably shouldn't be using a regex.
Upvotes: 2