Re: Regular Expression For Duplicate Words - Mailing list pgsql-general

From Shaozhong SHI
Subject Re: Regular Expression For Duplicate Words
Date
Msg-id CA+i5JwYJe_-oa15SOfDi4jpAJK_+K2A=o+i+WExmf2Np60G2mQ@mail.gmail.com
Whole thread Raw
In response to Re: Regular Expression For Duplicate Words  ("Peter J. Holzer" <hjp-pgsql@hjp.at>)
List pgsql-general
Hi, Peter,  Interesting.

On Thu, 3 Feb 2022 at 19:48, Peter J. Holzer <hjp-pgsql@hjp.at> wrote:
On 2022-02-02 08:00:00 +0000, Shaozhong SHI wrote:
> regex - Regular Expression For Duplicate Words - Stack Overflow
>
> Is there any example in Postgres?

It's pretty much the same as with other regexp dialects: User word
boundaries and a word character class to match any word and then use a
backreference to match a duplicate word. All the building blocks are
described on
https://www.postgresql.org/docs/current/functions-matching.html#FUNCTIONS-POSIX-REGEXP
and except for [[:<:]] and [[:>:]] for the word boundaries, they are
also pretty standard.

So

[[:<:]]        start of word
([[:alpha:]]+) one or more alphabetic characters in a capturing group
[[:>:]]        end of word
\W+            one or more non-word characters
[[:<:]]        start of word
\1             the content of the first (and only) capturing group
[[:>:]]        end of word

All together:

select * from t where t ~ '[[:<:]]([[:alpha:]]+)[[:>:]]\W[[:<:]]\1[[:>:]]';

Give a good example if you can.

Regards,

David 

pgsql-general by date:

Previous
From: A Shaposhnikov
Date:
Subject: Re: increasing effective_cache_size slows down join queries by a factor of 4000x
Next
From: saket bansal
Date:
Subject: pg_cron for vacuum - dynamic table set