pg.ddx.io pgsql-general@postgresql.org mailing list archive
help / color / mirror / Atom feedCan Postgres beat Oracle for regexp_count?
10+ messages / 4 participants
[nested] [flat]
* Can Postgres beat Oracle for regexp_count?
@ 2022-02-02 20:20 Shaozhong SHI <shishaozhong@gmail.com>
0 siblings, 1 reply; 10+ messages in thread
From: Shaozhong SHI @ 2022-02-02 20:20 UTC (permalink / raw)
To: pgsql-general <pgsql-general@lists.postgresql.org>
It has been found that regexp_count works brilliantly in Oracle.
However, it is not easy to replicate that in Postgres. The following codes
have been experimented but without any luck.
select regexp_matches('My High Street', '([A-Z][a-z]+[\s])', 'g')
select regexp_matches('My High Street', '([A-Z][a-z]+[\s]*)', 'g')
County occurrences of 'My High Street' in one of the following strings:
'My High Street' 1
'' 0
'My High Street My High Street' 2
Can anyone enlighten all of us?
Regards,
David
^ permalink raw reply [nested|flat] 10+ messages in thread
* Re: Can Postgres beat Oracle for regexp_count?
@ 2022-02-02 20:43 David G. Johnston <david.g.johnston@gmail.com>
parent: Shaozhong SHI <shishaozhong@gmail.com>
0 siblings, 1 reply; 10+ messages in thread
From: David G. Johnston @ 2022-02-02 20:43 UTC (permalink / raw)
To: Shaozhong SHI <shishaozhong@gmail.com>; +Cc: pgsql-general <pgsql-general@lists.postgresql.org>
On Wed, Feb 2, 2022 at 1:20 PM Shaozhong SHI <shishaozhong@gmail.com> wrote:
> It has been found that regexp_count works brilliantly in Oracle.
>
What query exactly did you execute in Oracle that you wish to see if an
equivalent can be formulated in PostgreSQL?
>
> However, it is not easy to replicate that in Postgres.
>
Given we don't have a regexp_count function this isn't surprising...
> The following codes have been experimented but without any luck.
>
> select regexp_matches('My High Street', '([A-Z][a-z]+[\s])', 'g')
>
> select regexp_matches('My High Street', '([A-Z][a-z]+[\s]*)', 'g')
>
See my first point.
David J.
^ permalink raw reply [nested|flat] 10+ messages in thread
* Re: Can Postgres beat Oracle for regexp_count?
@ 2022-02-02 22:26 Tom Lane <tgl@sss.pgh.pa.us>
parent: David G. Johnston <david.g.johnston@gmail.com>
0 siblings, 2 replies; 10+ messages in thread
From: Tom Lane @ 2022-02-02 22:26 UTC (permalink / raw)
To: David G. Johnston <david.g.johnston@gmail.com>; +Cc: Shaozhong SHI <shishaozhong@gmail.com>; pgsql-general <pgsql-general@lists.postgresql.org>
"David G. Johnston" <david.g.johnston@gmail.com> writes:
> Given we don't have a regexp_count function this isn't surprising...
FYI, it's there in HEAD.
In the meantime, you could possibly do something like
=# select count(*) from regexp_matches('My High Street', '([A-Z][a-z]+[\s])', 'g');
count
-------
2
(1 row)
(Note that 2 is the correct answer given that there's no space
after the third word; I trust Oracle agrees.)
regards, tom lane
^ permalink raw reply [nested|flat] 10+ messages in thread
* Re: Can Postgres beat Oracle for regexp_count?
@ 2022-02-03 05:26 Shaozhong SHI <shishaozhong@gmail.com>
parent: Tom Lane <tgl@sss.pgh.pa.us>
1 sibling, 2 replies; 10+ messages in thread
From: Shaozhong SHI @ 2022-02-03 05:26 UTC (permalink / raw)
To: Tom Lane <tgl@sss.pgh.pa.us>; +Cc: David G. Johnston <david.g.johnston@gmail.com>; pgsql-general <pgsql-general@lists.postgresql.org>
Hi, Tom, Lane,
On Wed, 2 Feb 2022 at 22:26, Tom Lane <tgl@sss.pgh.pa.us> wrote:
> "David G. Johnston" <david.g.johnston@gmail.com> writes:
> > Given we don't have a regexp_count function this isn't surprising...
>
> FYI, it's there in HEAD.
>
> In the meantime, you could possibly do something like
>
> =# select count(*) from regexp_matches('My High Street',
> '([A-Z][a-z]+[\s])', 'g');
> count
> -------
> 2
> (1 row)
>
> (Note that 2 is the correct answer given that there's no space
> after the third word; I trust Oracle agrees.)
>
> Can the whole 3 or 4 or 5 to be matched as 1?
>
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.
Regards, David
^ permalink raw reply [nested|flat] 10+ messages in thread
* Re: Can Postgres beat Oracle for regexp_count?
@ 2022-02-03 05:50 David G. Johnston <david.g.johnston@gmail.com>
parent: Shaozhong SHI <shishaozhong@gmail.com>
1 sibling, 0 replies; 10+ messages in thread
From: David G. Johnston @ 2022-02-03 05:50 UTC (permalink / raw)
To: Shaozhong SHI <shishaozhong@gmail.com>; +Cc: Tom Lane <tgl@sss.pgh.pa.us>; pgsql-general <pgsql-general@lists.postgresql.org>
On Wed, Feb 2, 2022 at 10:26 PM Shaozhong SHI <shishaozhong@gmail.com>
wrote:
>
> 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.
>
>
I'm too tired to find the documentation for why you saw your result but
basically you only have a single capturing parentheses pair and since
you've quantified that you end up with just the last capture that was found
- Street. If you want to capture the entire found expression you need to
capture the quantifier. So put parentheses around the entire regexp.
select regexp_matches('My High Street', '(([A-Z][a-z]+[\s]*)+)', 'g')
You now have a two element array, slots filled left-to-right based upon the
opening parenthesis. So {"My High Street",Street}
To get rid of the undesired Street and only return a single element array
you need to make the inner parentheses non-capturing.
select regexp_matches('My High Street', '((?:[A-Z][a-z]+[\s]*)+)', 'g')
David J.
^ permalink raw reply [nested|flat] 10+ messages in thread
* Re: Can Postgres beat Oracle for regexp_count?
@ 2022-02-03 05:59 Tom Lane <tgl@sss.pgh.pa.us>
parent: Shaozhong SHI <shishaozhong@gmail.com>
1 sibling, 1 reply; 10+ messages in thread
From: Tom Lane @ 2022-02-03 05:59 UTC (permalink / raw)
To: Shaozhong SHI <shishaozhong@gmail.com>; +Cc: David G. Johnston <david.g.johnston@gmail.com>; pgsql-general <pgsql-general@lists.postgresql.org>
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
^ permalink raw reply [nested|flat] 10+ messages in thread
* Re: Can Postgres beat Oracle for regexp_count?
@ 2022-02-03 09:17 Shaozhong SHI <shishaozhong@gmail.com>
parent: Tom Lane <tgl@sss.pgh.pa.us>
0 siblings, 1 reply; 10+ messages in thread
From: Shaozhong SHI @ 2022-02-03 09:17 UTC (permalink / raw)
To: Tom Lane <tgl@sss.pgh.pa.us>; +Cc: David G. Johnston <david.g.johnston@gmail.com>; pgsql-general <pgsql-general@lists.postgresql.org>
Many thanks, Tom,
select regexp_matches('My High Street', '(?:[A-Z][a-z]+[\s]*)+', 'g');
looks very interesting.
I did read the documentation, but found it is difficult to read.
Particularly, the documentation on the use ?: does not state clear sense.
There is only limited explanation on ?:.
Is it correct to say that this ?: construction of a regex can be applied
for checking whether cell values meet specifications?
Regards,
David
On Thu, 3 Feb 2022 at 05:59, Tom Lane <tgl@sss.pgh.pa.us> wrote:
> 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
>
^ permalink raw reply [nested|flat] 10+ messages in thread
* Re: Can Postgres beat Oracle for regexp_count?
@ 2022-02-03 13:00 David G. Johnston <david.g.johnston@gmail.com>
parent: Shaozhong SHI <shishaozhong@gmail.com>
0 siblings, 1 reply; 10+ messages in thread
From: David G. Johnston @ 2022-02-03 13:00 UTC (permalink / raw)
To: Shaozhong SHI <shishaozhong@gmail.com>; +Cc: Tom Lane <tgl@sss.pgh.pa.us>; pgsql-general <pgsql-general@lists.postgresql.org>
On Thursday, February 3, 2022, Shaozhong SHI <shishaozhong@gmail.com> wrote:
>
> Is it correct to say that this ?: construction of a regex can be applied
> for checking whether cell values meet specifications?
>
>>
>>
It does exactly what our examples shows it does. I don’t understand what
you mean above but if that helps you remember its purpose, great.
David J.
^ permalink raw reply [nested|flat] 10+ messages in thread
* Re: Can Postgres beat Oracle for regexp_count?
@ 2022-02-03 15:32 Shaozhong SHI <shishaozhong@gmail.com>
parent: David G. Johnston <david.g.johnston@gmail.com>
0 siblings, 0 replies; 10+ messages in thread
From: Shaozhong SHI @ 2022-02-03 15:32 UTC (permalink / raw)
To: David G. Johnston <david.g.johnston@gmail.com>; +Cc: Tom Lane <tgl@sss.pgh.pa.us>; pgsql-general <pgsql-general@lists.postgresql.org>
Hi, David,
Many thanks.
I am investigating into transformation of data quality validation through
automation with application of Postgres/PostGIS.
Regards,
David
On Thu, 3 Feb 2022 at 13:00, David G. Johnston <david.g.johnston@gmail.com>
wrote:
>
>
> On Thursday, February 3, 2022, Shaozhong SHI <shishaozhong@gmail.com>
> wrote:
>
>>
>> Is it correct to say that this ?: construction of a regex can be applied
>> for checking whether cell values meet specifications?
>>
>>>
>>>
> It does exactly what our examples shows it does. I don’t understand what
> you mean above but if that helps you remember its purpose, great.
>
> David J.
>
>
^ permalink raw reply [nested|flat] 10+ messages in thread
* Re: Can Postgres beat Oracle for regexp_count?
@ 2022-02-04 04:32 Merlin Moncure <mmoncure@gmail.com>
parent: Tom Lane <tgl@sss.pgh.pa.us>
1 sibling, 0 replies; 10+ messages in thread
From: Merlin Moncure @ 2022-02-04 04:32 UTC (permalink / raw)
To: Tom Lane <tgl@sss.pgh.pa.us>; +Cc: David G. Johnston <david.g.johnston@gmail.com>; Shaozhong SHI <shishaozhong@gmail.com>; pgsql-general <pgsql-general@lists.postgresql.org>
On Wed, Feb 2, 2022 at 4:26 PM Tom Lane <tgl@sss.pgh.pa.us> wrote:
>
> "David G. Johnston" <david.g.johnston@gmail.com> writes:
> > Given we don't have a regexp_count function this isn't surprising...
>
> FYI, it's there in HEAD.
>
> In the meantime, you could possibly do something like
>
> =# select count(*) from regexp_matches('My High Street', '([A-Z][a-z]+[\s])', 'g');
> count
> -------
> 2
> (1 row)
alternate version:
postgres=# select array_upper(regexp_split_to_array('My High Street My
High Street', 'My High Street'), 1) - 1;
?column?
──────────
2
can help to slide this into complex queries a little bit easier by
avoiding the aggregation :-).
merlin
^ permalink raw reply [nested|flat] 10+ messages in thread
end of thread, other threads:[~2022-02-04 04:32 UTC | newest]
Thread overview: 10+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2022-02-02 20:20 Can Postgres beat Oracle for regexp_count? Shaozhong SHI <shishaozhong@gmail.com>
2022-02-02 20:43 ` David G. Johnston <david.g.johnston@gmail.com>
2022-02-02 22:26 ` Tom Lane <tgl@sss.pgh.pa.us>
2022-02-03 05:26 ` Shaozhong SHI <shishaozhong@gmail.com>
2022-02-03 05:50 ` David G. Johnston <david.g.johnston@gmail.com>
2022-02-03 05:59 ` Tom Lane <tgl@sss.pgh.pa.us>
2022-02-03 09:17 ` Shaozhong SHI <shishaozhong@gmail.com>
2022-02-03 13:00 ` David G. Johnston <david.g.johnston@gmail.com>
2022-02-03 15:32 ` Shaozhong SHI <shishaozhong@gmail.com>
2022-02-04 04:32 ` Merlin Moncure <mmoncure@gmail.com>
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