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 1ijPbG-0002AG-CM for pgsql-general@arkaria.postgresql.org; Mon, 23 Dec 2019 15:26:26 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1ijPbE-0007jC-SS for pgsql-general@arkaria.postgresql.org; Mon, 23 Dec 2019 15:26:24 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1ijPbE-0007h4-IX for pgsql-general@lists.postgresql.org; Mon, 23 Dec 2019 15:26:24 +0000 Received: from sss.pgh.pa.us ([66.207.139.130]) by magus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1ijPb8-0000ae-7M for pgsql-general@postgresql.org; Mon, 23 Dec 2019 15:26:24 +0000 Received: from sss1.sss.pgh.pa.us (localhost [127.0.0.1]) by sss.pgh.pa.us (8.14.4/8.14.4) with ESMTP id xBNFQF1x029226; Mon, 23 Dec 2019 10:26:15 -0500 From: Tom Lane To: "Daniel Verite" cc: pgsql-general@postgresql.org Subject: Re: Mixing greediness in regexp_matches In-reply-to: <28143.1577114140@sss.pgh.pa.us> References: <306b726b-f185-4668-bffe-ac8e7f78878e@manitou-mail.org> <28143.1577114140@sss.pgh.pa.us> Comments: In-reply-to Tom Lane message dated "Mon, 23 Dec 2019 10:15:40 -0500" MIME-Version: 1.0 Content-Type: text/plain; charset="us-ascii" Content-ID: <29224.1577114775.1@sss.pgh.pa.us> Content-Transfer-Encoding: quoted-printable Date: Mon, 23 Dec 2019 10:26:15 -0500 Message-ID: <29225.1577114775@sss.pgh.pa.us> List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk "Daniel Verite" writes: >> The basic idea is to iterate on the rows produced by >> regexp_matches(string, '(.*?)(foo|bar|foobar)', 'g') >> to break down the string into pairs of (non-matching segment, >> matching segment) so that a final result can be assembled >> from that (setting aside the last non-matching segment, that >> can be retrieved in a final step). BTW, just to think outside the box a bit, I wonder whether you couldn't build this out of regexp_split_to_array: regression=3D# select regexp_split_to_array('junkfoolbarfoolishfoobarmore'= , 'foo|bar|foobar'); regexp_split_to_array = ----------------------- {junk,l,"",lish,more} (1 row) 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. 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. regards, tom lane