Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtp (Exim 4.80) (envelope-from ) id 1ZMeJp-00059y-AR for pgsql-bugs@arkaria.postgresql.org; Tue, 04 Aug 2015 15:39:57 +0000 Received: from localhost ([127.0.0.1] helo=postgresql.org) by malur.postgresql.org with smtp (Exim 4.84) (envelope-from ) id 1ZMeJo-0001Vq-Np for pgsql-bugs@arkaria.postgresql.org; Tue, 04 Aug 2015 15:39:56 +0000 Received: from makus.postgresql.org ([2001:4800:1501:1::229]) by malur.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.84) (envelope-from ) id 1ZMeJm-0001Sq-4Z for pgsql-bugs@postgresql.org; Tue, 04 Aug 2015 15:39:54 +0000 Received: from sss.pgh.pa.us ([66.207.139.130]) by makus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.84) (envelope-from ) id 1ZMeJh-0005lF-Sj for pgsql-bugs@postgresql.org; Tue, 04 Aug 2015 15:39:52 +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 t74FdkAY032325; Tue, 4 Aug 2015 11:39:46 -0400 From: Tom Lane To: christian_maechler@hotmail.com cc: "David G. Johnston" , "pgsql-bugs@postgresql.org" Subject: Re: BUG #13538: REGEX non-greedy is working incorrectly (and also greedy matches fail if non-greedy is present) In-reply-to: <26003.1438700337@sss.pgh.pa.us> References: <20150803224450.1115.8803@wrigleys.postgresql.org>, <26003.1438700337@sss.pgh.pa.us> Comments: In-reply-to Tom Lane message dated "Tue, 04 Aug 2015 10:58:57 -0400" Date: Tue, 04 Aug 2015 11:39:46 -0400 Message-ID: <32324.1438702786@sss.pgh.pa.us> X-Pg-Spam-Score: -2.1 (--) List-Archive: List-Help: List-ID: List-Owner: List-Post: List-Subscribe: List-Unsubscribe: X-Mailing-List: pgsql-bugs Precedence: bulk Sender: pgsql-bugs-owner@postgresql.org I wrote: > As David says, these examples appear to be following what's stated in > http://www.postgresql.org/docs/9.4/static/functions-matching.html#POSIX-MATCHING-RULES > The Spencer regex engine we use has a notion of greediness or > non-greediness of the entire regex, and further that that takes precedence > for determining the overall match length over greediness of individual > subexpressions. That behavior might be inconvenient for this particular > use-case, but that doesn't make it a bug. BTW, perhaps it would be worth adding an example to that section that shows how to control this behavior. The trick is obvious once you've seen it, but not so much otherwise: you add something to the start of the regex that establishes the overall greediness you want, but can never actually match any characters. "\0*" or "\0*?" will work fine in Postgres use-cases since there can never be a NUL character in the data. regression=# select regexp_matches('abc01234xyz', '(.*)(\d+)(.*)'); regexp_matches ----------------- {abc0123,4,xyz} (1 row) regression=# select regexp_matches('abc01234xyz', '(.*?)(\d+)(.*)'); regexp_matches ---------------- {abc,0,""} (1 row) regression=# select regexp_matches('abc01234xyz', '\0*(.*?)(\d+)(.*)'); regexp_matches ----------------- {abc,01234,xyz} (1 row) regards, tom lane -- Sent via pgsql-bugs mailing list (pgsql-bugs@postgresql.org) To make changes to your subscription: http://www.postgresql.org/mailpref/pgsql-bugs