tags:

views:

70

answers:

2

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
+2  A: 

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.

Tim Sylvester
Would you have any insight on speeds of your solution vs. just calling regex_match 3 separate times?
yankee2905
I'd think if you are that worried about speed, you should be using a custom lexer (or lex) instead of a regex. More likely it isn't worth worrying about.
T.E.D.
A: 

If you want to analyze your text matching to that level of detail, perhaps you should consider using boost's Spirit instead of regex.

T.E.D.