in reply to nested pattern matching
I've never encountered such a problem, but could you use pos, capture the pattern, and use length on $1 to calculate what you need? You'd have to use the /g modifier, but I think it might work.