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 1hPf1T-0001DM-K2 for pgsql-bugs@arkaria.postgresql.org; Sun, 12 May 2019 03:19:35 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1hPf1R-00009e-2V for pgsql-bugs@arkaria.postgresql.org; Sun, 12 May 2019 03:19:33 +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 1hPf1Q-00009S-RY for pgsql-bugs@lists.postgresql.org; Sun, 12 May 2019 03:19:32 +0000 Received: from lungold.riddles.org.uk ([82.68.208.19]) by magus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1hPf1O-0004sM-9o for pgsql-bugs@lists.postgresql.org; Sun, 12 May 2019 03:19:32 +0000 Received: from [192.168.127.1] (port=52749 helo=caithnard.riddles.org.uk) by lungold.riddles.org.uk with esmtps (TLSv1.2:ECDHE-RSA-AES256-GCM-SHA384:256) (Exim 4.88 (FreeBSD)) (envelope-from ) id 1hPf1I-000Lbh-8B; Sun, 12 May 2019 03:19:24 +0000 Received: from [127.0.0.1] (port=51097 helo=caithnard.riddles.org.uk) by caithnard.riddles.org.uk with esmtp (Exim 4.89 (FreeBSD)) (envelope-from ) id 1hPf1H-0008Ln-JN; Sun, 12 May 2019 03:19:23 +0000 From: Andrew Gierth To: Tom Lane Cc: "Daniel Verite" , "Robert Schreiber" , pgsql-bugs@lists.postgresql.org Subject: Re: PostgreSQL 9.3.5 substring(text from pattern for escape) bug In-Reply-To: <32565.1557590361@sss.pgh.pa.us> (Tom Lane's message of "Sat, 11 May 2019 11:59:21 -0400") Message-ID: <87mujsgz14.fsf@news-spur.riddles.org.uk> References: <785708a0-a64c-42ee-9173-f596c1fc92ec@manitou-mail.org> <32565.1557590361@sss.pgh.pa.us> User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/25.1 (berkeley-unix) Date: Sun, 12 May 2019 04:19:23 +0100 MIME-Version: 1.0 Content-Type: text/plain List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk >>>>> "Tom" == Tom Lane writes: >>> What appears to be happening is that the _Q? on the tail_ is being >>> matched, but _the Q is also being included in the collected data.__ >> You seem to expect that % must be non-greedy and let the final Q? >> match 1 Q instead of 0, but there doesn't appear to be anything in >> the doc that supports this interpretation. In fact, it mentions that >> "%" is comparable to ".*" in POSIX regular expressions, and the >> latter _is_ greedy. Tom> Right. You could get the behavior you want using a non-greedy Tom> quantifier, but you'd have to use the POSIX regexp functions, not Tom> substring(). I looked up the spec on this point. As far as I can see, we're not following it, but neither does the spec do what the OP wanted; in fact the result should have included the _leading_ Q as well as the trailing one. The relevant part of SQL2016 seems to be this: 6.32 General Rules 6) If is specified, then: [...rules that split the pattern into 'R1#"R2#"R3' omitted...] h) Otherwise, the result S of the is computed as follows: i) Let S1 be the shortest initial substring of C such that there is a substring S23 of C such that the value of the following is True: 'C' = 'S1' || 'S23' AND 'S1' SIMILAR TO 'R1' ESCAPE 'E' AND 'S23' SIMILAR TO '(R2R3)' ESCAPE 'E' ii) Let S3 be the shortest final substring of S23 such that there is a substring S2 of S23 such that the value of the following is True: 'S23' = 'S2' || 'S3' AND 'S2' SIMILAR TO 'R2' ESCAPE 'E' AND 'S3' SIMILAR TO 'R3' ESCAPE 'E' iii) The result of the is S2. -- Andrew (irc:RhodiumToad)