From: Tom Lane <tgl@sss.pgh.pa.us>
To: Shaozhong SHI <shishaozhong@gmail.com>
Cc: David G. Johnston <david.g.johnston@gmail.com>
Cc: pgsql-general <pgsql-general@lists.postgresql.org>
Subject: Re: Can Postgres beat Oracle for regexp_count?
Date: Thu, 03 Feb 2022 00:59:36 -0500
Message-ID: <205525.1643867976@sss.pgh.pa.us> (raw)
In-Reply-To: <CA+i5JwZxxVy0uwg9bKYQOs=PZ617Xr7xTaPm4eqsrzsBz-BxNw@mail.gmail.com>
References: <CA+i5JwaC87tdr4s=mzkBqPOrqp+sPHVEA+HmwRX0hh1ZBUNKkg@mail.gmail.com>
<CAKFQuwZtR5mMG5Girp1qkX0YbBaEG9pZ+oreqXgfp9y-SnRkjQ@mail.gmail.com>
<104523.1643840782@sss.pgh.pa.us>
<CA+i5JwZxxVy0uwg9bKYQOs=PZ617Xr7xTaPm4eqsrzsBz-BxNw@mail.gmail.com>
Shaozhong SHI <shishaozhong@gmail.com> 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=# select regexp_matches('My High Street', '(?:[A-Z][a-z]+[\s]*)+', 'g');
regexp_matches
--------------------
{"My High Street"}
(1 row)
or you could do
regression=# 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=# 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#FUNCTIONS-POSIX-REGEXP
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Reply to all the recipients from your mail client; the link fills in
the headers below: reply via email
Some clients, webmail especially, drop In-Reply-To and References from
a link, and the reply then starts a new thread: use the mbox below.
To: pgsql-general@postgresql.org
Cc: tgl@sss.pgh.pa.us, shishaozhong@gmail.com, david.g.johnston@gmail.com
Subject: Re: Can Postgres beat Oracle for regexp_count?
In-Reply-To: <205525.1643867976@sss.pgh.pa.us>
References: <CA+i5JwaC87tdr4s=mzkBqPOrqp+sPHVEA+HmwRX0hh1ZBUNKkg@mail.gmail.com> <CAKFQuwZtR5mMG5Girp1qkX0YbBaEG9pZ+oreqXgfp9y-SnRkjQ@mail.gmail.com> <104523.1643840782@sss.pgh.pa.us> <CA+i5JwZxxVy0uwg9bKYQOs=PZ617Xr7xTaPm4eqsrzsBz-BxNw@mail.gmail.com> <205525.1643867976@sss.pgh.pa.us>
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox