From: Tom Lane <tgl@sss.pgh.pa.us>
To: Simon Ellmann <simon.ellmann@tum.de>
Cc: pgsql-bugs@lists.postgresql.org <pgsql-bugs@lists.postgresql.org>
Subject: Re: regexp_replace not respecting greediness
Date: Fri, 19 Sep 2025 12:01:53 -0400
Message-ID: <2134893.1758297713@sss.pgh.pa.us> (raw)
In-Reply-To: <1B268DC1-F058-4243-B6F8-71451FB68F08@tum.de>
References: <1B268DC1-F058-4243-B6F8-71451FB68F08@tum.de>
Simon Ellmann <simon.ellmann@tum.de> writes:
> With the following regular expression, the second .* seems to match non-greedily although (if I am correct) it should match greedily:
> postgres=# SELECT REGEXP_REPLACE('jane.smith@example.com', '.*?@.*', 'ab');
This is correct according to the rules given at
https://www.postgresql.org/docs/current/functions-matching.html#POSIX-MATCHING-RULES
specifically that "A branch — that is, an RE that has no top-level |
operator — has the same greediness as the first quantified atom in it
that has a greediness attribute." Because of that, the RE as a whole
is non-greedy and will match the shortest not longest amount of text
overall. The discussion in that manual section shows what to do
when you don't like the results.
> Other database systems (e.g., DuckDB, Umbra) match the whole input:
If your complaint is "but it's not like Perl!", I suggest using
a plperl function to do your regexp work.
regards, tom lane
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Reply to all the recipients using the --to and --cc options:
reply via email
To: pgsql-bugs@postgresql.org
Cc: tgl@sss.pgh.pa.us, simon.ellmann@tum.de, pgsql-bugs@lists.postgresql.org
Subject: Re: regexp_replace not respecting greediness
In-Reply-To: <2134893.1758297713@sss.pgh.pa.us>
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox