Regular Expressions with Backreferences: Polynomial-Time Matching Techniques
Markus L. Schmid
TL;DR
This work addresses the intractability of matching regular expressions with backreferences by introducing two principled approaches: bounding the number of simultaneously active variables via active variable degree (avd) to achieve polynomial-time matching in the input word, and enforcing memory-determinism (md-MFA) to attain near-linear data complexity with a polynomial overhead. Both approaches are grounded in memory automata (MFA) and leverage a Thompson-like translation from regex to automata to analyze structure and control nondeterminism. The paper establishes formal definitions (avd, savd, md-MFA) and derives rigorous complexity results, including polynomial-time matching for avd-bounded regex, $O(|w|p(| ||))$-type matching for md-MFA, and $(co)NP$-hardness results for stronger notions and for verifying certain properties (e.g., savd and synchronised-ness). These findings reveal a meaningful trade-off between expressiveness and tractable matching, showing that memory-deterministic variants strictly extend classical regular expressions while enabling efficient algorithms, albeit with nontrivial checks for determinism.
Abstract
Regular expressions with backreferences (regex, for short), as supported by most modern libraries for regular expression matching, have an NP-complete matching problem. We define a complexity parameter of regex, called active variable degree, such that regex with this parameter bounded by a constant can be matched in polynomial-time. Moreover, we formulate a novel type of determinism for regex (on an automaton-theoretic level), which yields the class of memory-deterministic regex that can be matched in time O(|w|p(|r|)) for a polynomial p (where r is the regex and w the word). Natural extensions of these concepts lead to properties of regex that are intractable to check.
