Received: from makus.postgresql.org (makus.postgresql.org [98.129.198.125]) by mail.postgresql.org (Postfix) with ESMTP id 1F2401CB8ED9 for ; Thu, 5 Jan 2012 20:32:35 -0400 (AST) Received: from mahout.postgresql.org ([98.129.198.123]) by makus.postgresql.org with esmtp (Exim 4.72) (envelope-from ) id 1RixjS-0003TK-A4 for pgsql-bugs@postgresql.org; Fri, 06 Jan 2012 00:32:34 +0000 Received: from [204.145.120.229] (helo=wrigleys.postgresql.org) by mahout.postgresql.org with esmtp (Exim 4.72) (envelope-from ) id 1RixjF-0003EC-Q2 for pgsql-bugs@postgresql.org; Fri, 06 Jan 2012 00:32:18 +0000 Received: from www-data by wrigleys.postgresql.org with local (Exim 4.72) (envelope-from ) id 1RixjF-0006gP-Dt for pgsql-bugs@postgresql.org; Fri, 06 Jan 2012 00:32:17 +0000 Date: Fri, 06 Jan 2012 00:32:17 +0000 Message-Id: Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Subject: BUG #6381: Incorrect greediness behavior in certain regular expressions To: pgsql-bugs@postgresql.org From: code@phaedrusdeinus.org X-Pg-Spam-Score: -1.9 (-) X-Archive-Number: 201201/38 X-Sequence-Number: 32105 The following bug has been logged on the website: Bug reference: 6381 Logged by: john melesky Email address: code@phaedrusdeinus.org PostgreSQL version: 9.1.1 Operating system: x86_64-pc-linux-gnu Description: = This simple regexp returns correctly (that is, (.*?) matches 'blahblah.com'): =3D# select regexp_matches('http://blahblah.com/asdf', 'http://(.*?)(/|%2f|$)'); regexp_matches = ------------------ {blahblah.com,/} This, more complex/complete version, matches greedily, which is incorrect: =3D# select regexp_matches('http://blahblah.com/asdf', 'http(s?)(:|%3a)(//|%2f%2f)(.*?)(/|%2f|$)'); regexp_matches = -------------------------------- {"",:,//,blahblah.com/asdf,""} (That is, (.*?) matches 'blahblah.com/asdf') The problem appears to be the inclusion of '$' in the final paren group. So, this works: select regexp_matches('http://blahblah.com/asdf', 'http(s?)(:|%3a)(//|%2f%2f)(.*?)(/|%2f)'); regexp_matches = -------------------------- {"",:,//,blahblah.com,/}