Thank you Francisco, that's a clever idea. However, I don't think this would reduce the complexity since the target pattern can contain 1) regular back-references (referencing to matches of its own) 2) the special source text references I mentioned Obviously, these will have to be written in a different way and this I believe brings me back to start (or in other words, it's not a silver bullet obviating the need to rewrite the target pattern manually). I will probably end up writing a function in PL/Perl which Tom Lane suggested since I'm apparently not skilled in SQL enough to be able to do it using a single query without using custom functions. -- View this message in context: http://postgresql.1045698.n5.nabble.com/Emulating-flexible-regex-replace-tp5824058p5824109.html Sent from the PostgreSQL - general mailing list archive at Nabble.com. -- Sent via pgsql-general mailing list (pgsql-general@xxxxxxxxxxxxxx) To make changes to your subscription: http://www.postgresql.org/mailpref/pgsql-general