Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1nFV9j-0002KM-VQ for pgsql-general@arkaria.postgresql.org; Thu, 03 Feb 2022 05:59:43 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1nFV9h-0003LB-RB for pgsql-general@arkaria.postgresql.org; Thu, 03 Feb 2022 05:59:41 +0000 Received: from makus.postgresql.org ([2001:4800:3e1:1::229]) by malur.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1nFV9h-0003L2-Gz for pgsql-general@lists.postgresql.org; Thu, 03 Feb 2022 05:59:41 +0000 Received: from sss.pgh.pa.us ([66.207.139.130]) by makus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1nFV9e-0002BQ-MS for pgsql-general@lists.postgresql.org; Thu, 03 Feb 2022 05:59:40 +0000 Received: from sss1.sss.pgh.pa.us (localhost [127.0.0.1]) by sss.pgh.pa.us (8.15.2/8.15.2) with ESMTP id 2135xaLg205526; Thu, 3 Feb 2022 00:59:37 -0500 From: Tom Lane To: Shaozhong SHI cc: "David G. Johnston" , pgsql-general Subject: Re: Can Postgres beat Oracle for regexp_count? In-reply-to: References: <104523.1643840782@sss.pgh.pa.us> Comments: In-reply-to Shaozhong SHI message dated "Thu, 03 Feb 2022 05:26:14 +0000" MIME-Version: 1.0 Content-Type: text/plain; charset="us-ascii" Content-ID: <205524.1643867976.1@sss.pgh.pa.us> Content-Transfer-Encoding: quoted-printable Date: Thu, 03 Feb 2022 00:59:36 -0500 Message-ID: <205525.1643867976@sss.pgh.pa.us> List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk Shaozhong SHI writes: > The following has been attempted but no luck. > select regexp_matches('My High Street', '([A-Z][a-z]+[\s]*)+', 'g') > It is intended to match 'My High Street, but it turned out only 'Street' > was matched. You've got the parentheses in the wrong place, ie inside not outside the "+" quantifier. Per the fine manual [1], the result is determined by the last match of quantified capturing parens. You could avoid using any capturing parens, so that the result is the whole match: regression=3D# select regexp_matches('My High Street', '(?:[A-Z][a-z]+[\s]= *)+', 'g'); regexp_matches = -------------------- {"My High Street"} (1 row) or you could do regression=3D# select regexp_matches('My High Street', '(([A-Z][a-z]+[\s]*= )+)', 'g'); regexp_matches = --------------------------- {"My High Street",Street} (1 row) but then you have two sets of capturing parens and you get results for both, so you might prefer regression=3D# select regexp_matches('My High Street', '((?:[A-Z][a-z]+[\s= ]*)+)', 'g'); regexp_matches = -------------------- {"My High Street"} (1 row) In any case, there's no substitute for reading the manual. regards, tom lane [1] https://www.postgresql.org/docs/current/functions-matching.html#FUNCTI= ONS-POSIX-REGEXP