Is it possible to implement capture groups with DFA-based regular expressions while maintaining a linear time complexity with respect to the input length?
Intuitively I
Is it possible to implement capture groups with DFA-based regular expressions while maintaining a linear time complexity with respect to the input length?
Yes - at least when the capture groups are deterministic. Consider the example regex /a|(a)/
; matching that against the input "a"
could either produce a captured group or none.
I think that capture groups could be based on a theoretical foundation using finite state transducers, which are like automatons but also may output strings while changing states. You may echo the input, but surround each capture group with parenthesis for example.
Intuitively I think not, because the subset construction procedure does not know which capture group it may have landed inside, but this is the first time I've realized this may be a potential problem, so I don't know.
Indeed, this is a problem. I think you can solve it by tagging your sets with the capture state, and similarly distinguish the states of your result DFA. You may fail to produce a fully deterministic automaton for regular expression like the above, as Wikipedia writes: "some non-deterministic transducers do not admit equivalent deterministic transducers".
However, a modification of the subset construction procedure is possible, see Determinization of Transducers. Their algorithm seems to revolve around the following:
local ambiguities […] are solved by delaying the outputs as far as needed, until these symbols can be written out deterministically.
For example, the regexes /ab|(a)c/
and even /(a[bc])|ad/
can be resolved into deterministic transducers. Notice that their memory representation may be much larger than if they had no capture groups.