Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1ijQIE-00048C-8O for pgsql-general@arkaria.postgresql.org; Mon, 23 Dec 2019 16:10:50 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1ijQID-0005Fy-0M for pgsql-general@arkaria.postgresql.org; Mon, 23 Dec 2019 16:10:49 +0000 Received: from makus.postgresql.org ([2001:4800:3e1:1::229]) by malur.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1ijQIC-0005Fr-J3 for pgsql-general@lists.postgresql.org; Mon, 23 Dec 2019 16:10:48 +0000 Received: from mail.verite.pro ([185.16.44.216]) by makus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1ijQI9-0002Sx-Hc for pgsql-general@postgresql.org; Mon, 23 Dec 2019 16:10:47 +0000 Received: from genius-map.net (verite.pro [195.154.189.246]) by mail.verite.pro (Postfix) with ESMTP id 680548C0187; Mon, 23 Dec 2019 17:03:25 +0100 (CET) Received: by genius-map.net (Postfix, from userid 1001) id E196138A0E5C; Mon, 23 Dec 2019 17:10:42 +0100 (CET) Content-Type: text/plain; charset="iso-8859-15" Content-Disposition: inline Content-Transfer-Encoding: quoted-printable MIME-Version: 1.0 Subject: Re: Mixing greediness in regexp_matches From: "Daniel Verite" To: "Tom Lane" Cc: pgsql-general@postgresql.org In-Reply-To: <29225.1577114775@sss.pgh.pa.us> Date: Mon, 23 Dec 2019 17:10:41 +0100 Message-Id: <90347d36-4b3c-4806-bd99-e5fae2cfad71@manitou-mail.org> X-Mailer: Manitou v1.7.3 List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk Tom Lane wrote: > regression=3D# select regexp_split_to_array('junkfoolbarfoolishfoobarmore= ', > 'foo|bar|foobar'); > regexp_split_to_array=20 > ----------------------- > {junk,l,"",lish,more} > (1 row) >=20 > The idea would be to iterate over the array elements, tracking the > corresponding position in the source string, and re-discovering at > each break which of the original alternatives must've matched. >=20 > It's sort of annoying that we don't have a simple "regexp_location" > function that would give you back the starting position of the > first match. It occurred to me too that regexp_split_to_table or array would make this problem really easy if only it had a mode to capture and return the matched parts too. FWIW, in plperl, there's a simple solution: $string =3D~ s/(foobar|foo|...)/$replace{$1}/g when %replace is a hash of the substitutions %(foo=3D>baz,...). The strings in the alternation are tested in their order of appearance, so you can choose to be greedy or not by just sorting them by length. Best regards, --=20 Daniel V=E9rit=E9 PostgreSQL-powered mailer: http://www.manitou-mail.org Twitter: @DanielVerite