In fact, there is no more accurate solution, because it is here that we leave the region of regular languages ββand begin to require a more complex automaton with some kind of memory. (Backrefs would do this, except that backref expands to literally match the previous part of the line, not "this, but only if that was agreed.")
Sometimes it is possible to use a two-stage process instead, replacing complex X one character, which, as you know, is not present in the source text (control characters may be suitable for this), which simplifies the second stage of the match. Not always possible; depends on what you match.
Donal fellows
source share