In reference to this bug report about $regmlex() groups:

For the benefit of future readers, you can get the group number for e.g. $regml(2) using $regml(2).group so code like this would work:
Code:
//noop $regex(test regml,/(test)\s(a\s)?(regml)/)
var %i 1 | while (%i <= $regml(0)) { set -l %g $+ $regml(%i).group = $regml(%i) | inc %i }
echo -a Number of groups captured: $regml(0) - Group 1: %g1 - Group 2: %g2 - Group 3: %g3

which results in the groups being in the correct positions:
Code:
Number of groups captured: 2 - Group 1: test - Group 2: - Group 3: regml