This is likely to be a very hard problem, so hard that I don't think you should expect a useful solution that can handle all possible regexes.
The extended regular expressions supported by Python's re.sub
are very expressive and powerful, which makes the verification problems involving them very hard. These advanced features (backreferences, capture groups, lookahead, lookbehind, etc.) make these regexes very expressive. For instance, it is known that the problem of testing whether a string matches a regex (that might contain backreferences) is NP-complete. As another example, it is known that testing equivalence of two such regexes is undecidable, as is testing whether such a regex matches every string, or testing whether such a regex matches a regular language. For proofs, see the following paper:
Extended Regular Expressions: Succinctness and Decidability. Dominik D. Freydenberger. Theory of Computing Systems, vol 53, pp.159-193, 2013.
The takeaway is that verification problems involving this type of regex tend to be very hard. I don't know of anyone who have studied idempotency of a sequence of re.sub
operations, but I would imagine that likely it is also very hard... possibly also undecidable.
I would suggest that you figure out what your actual goals are, and then see if you can find some alternative approach to achieve your real goals. Perhaps you only need to reason about a subset of regexes (e.g., only those that can be represented by a finite-state machine / regular transducer)? Perhaps it suffices to use testing to check idempotency, without having any formal guarantees? Perhaps you can find a way to structure your code so you don't need to verify this property? It is hard to know, without more context.
See also https://cstheory.stackexchange.com/q/448/5038, https://www.npopov.com/2012/06/15/The-true-power-of-regular-expressions.html, Which languages do Perl-compatible regular expressions recognize?, What is the computational complexity of "real-life" regular expressions?.