Exclude subexpression from regexec in c++
- by wyatt
Suppose I was trying to match the following expression using regex.h in C++, and trying to obtain the subexpressions contained:
/^((1|2)|3) (1|2)$/
Suppose it were matched against the string "3 1", the subexpressions would be:
"3 1"
"3"
"1"
If, instead it were matched against the string "2 1", the subexpressions would be:
"2 1"
"2"
"2"
"1"
Which means that, depending on how the first subexpression evaluates, the final one is in a different element in the pmatch array. I realise this particular example is trivial, as I could remove one of the sets of brackets, or grab the last element of the array, but it becomes problematic in more complicated expressions.
Suppose all I want are the top-level subexpressions, the ones which aren't subexpressions of other subexpressions. Is there any way to only get them? Or, alternatively, to know how many subexpressions are matched within a subexpression, so that I can traverse the array irrespective of how it evaluates?
Thanks