Suppose I tried to match the following expression using regex.h in C ++ and try to get the subexpressions contained in it:
/^((1|2)|3) (1|2)$/
Suppose it was matched with the string "3 1", the subexpressions would be:
"3 1" "3" "1"
If instead it was matched with the string "2 1", the subexpressions will be:
"2 1" "2" "2" "1"
This means that depending on how the first subexpression is evaluated, the last is in another element of the pmatch array. I understand that this particular example is trivial, as I could remove one of the sets of brackets or capture the last element of the array, but it becomes problematic in more complex expressions.
Suppose all I need is top-level subexpressions, those that are not subexpressions of other subexpressions. Is there any way to get them? Or, alternatively, to find out how many subexpressions are matched in the subexpression so that I can traverse the array no matter how it evaluates?
thanks
source share