Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtp (Exim 4.84_2) (envelope-from ) id 1bZI9M-0003o0-DZ for pgsql-bugs@arkaria.postgresql.org; Mon, 15 Aug 2016 13:41:56 +0000 Received: from localhost ([127.0.0.1] helo=postgresql.org) by malur.postgresql.org with smtp (Exim 4.84_2) (envelope-from ) id 1bZI9M-0004w8-0L for pgsql-bugs@arkaria.postgresql.org; Mon, 15 Aug 2016 13:41:56 +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_SHA384:256) (Exim 4.84_2) (envelope-from ) id 1bZI9L-0004w2-N0 for pgsql-bugs@postgresql.org; Mon, 15 Aug 2016 13:41:55 +0000 Received: from sss.pgh.pa.us ([66.207.139.130]) by magus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.84_2) (envelope-from ) id 1bZI9H-0003Tb-K9 for pgsql-bugs@postgresql.org; Mon, 15 Aug 2016 13:41:55 +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 u7FDfkIB004425; Mon, 15 Aug 2016 09:41:46 -0400 From: Tom Lane To: "Foster, Russell" cc: "pgsql-bugs@postgresql.org" Subject: Re: 9.5.3: substring: regex greedy operator not picking up chars as expected In-reply-to: References: Comments: In-reply-to "Foster, Russell" message dated "Mon, 15 Aug 2016 11:53:29 -0000" Date: Mon, 15 Aug 2016 09:41:46 -0400 Message-ID: <4424.1471268506@sss.pgh.pa.us> X-Pg-Spam-Score: -2.5 (--) 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 "Foster, Russell" writes: > For the following query: > select substring('>772' from '.*?[0-9]+') > I would expect the output to be '>772', but it is '>7'. As David pointed out, that's what you get because the RE as a whole is considered to be non-greedy, ie you get the shortest overall match. However, you can adjust that by decorating the RE: # select substring('>772' from '(.*?[0-9]+){1,1}'); substring ----------- >772 (1 row) Now it's longest-overall, but the .*? part is still shortest-match, so it doesn't consume any digits. However, I suspect that still is not quite what you want, because it consumes too much in cases like: # select substring('>772foo444' from '(.*?[0-9]+){1,1}'); substring ------------ >772foo444 (1 row) There's probably really no way out of that except to be less lazy about writing the pattern: # select substring('>772foo444' from '([^0-9]*?[0-9]+){1,1}'); substring ----------- >772 (1 row) and in that formulation, of course, greediness doesn't really matter because there is only one way to match. # select substring('>772foo444' from '[^0-9]*[0-9]+'); substring ----------- >772 (1 row) See https://www.postgresql.org/docs/9.5/static/functions-matching.html#POSIX-MATCHING-RULES 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