Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1uzdYb-005ijc-Dq for pgsql-bugs@arkaria.postgresql.org; Fri, 19 Sep 2025 16:01:57 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.94.2) (envelope-from ) id 1uzdYa-008EjL-1B for pgsql-bugs@arkaria.postgresql.org; Fri, 19 Sep 2025 16:01:56 +0000 Received: from makus.postgresql.org ([2001:4800:3e1:1::229]) by malur.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1uzdYZ-008EjB-Ok for pgsql-bugs@lists.postgresql.org; Fri, 19 Sep 2025 16:01:55 +0000 Received: from sss.pgh.pa.us ([68.162.161.243]) by makus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.96) (envelope-from ) id 1uzdYX-001IM5-1l for pgsql-bugs@lists.postgresql.org; Fri, 19 Sep 2025 16:01:54 +0000 Received: from sss1.sss.pgh.pa.us (localhost [127.0.0.1]) by sss.pgh.pa.us (8.15.2/8.15.2) with ESMTP id 58JG1rRA2134894; Fri, 19 Sep 2025 12:01:53 -0400 From: Tom Lane To: Simon Ellmann cc: "pgsql-bugs@lists.postgresql.org" Subject: Re: regexp_replace not respecting greediness In-reply-to: <1B268DC1-F058-4243-B6F8-71451FB68F08@tum.de> References: <1B268DC1-F058-4243-B6F8-71451FB68F08@tum.de> Comments: In-reply-to Simon Ellmann message dated "Fri, 19 Sep 2025 12:36:20 -0000" MIME-Version: 1.0 Content-Type: text/plain; charset="UTF-8" Content-ID: <2134892.1758297712.1@sss.pgh.pa.us> Content-Transfer-Encoding: quoted-printable Date: Fri, 19 Sep 2025 12:01:53 -0400 Message-ID: <2134893.1758297713@sss.pgh.pa.us> List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk Simon Ellmann writes: > With the following regular expression, the second .* seems to match non-= greedily although (if I am correct) it should match greedily: > postgres=3D# 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-MATC= HING-RULES specifically that "A branch =E2=80=94 that is, an RE that has no top-level= | operator =E2=80=94 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