pg.ddx.io  pgsql-general@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Peter J. Holzer <hjp-pgsql@hjp.at>
To: pgsql-general@lists.postgresql.org
Subject: Re: Regular Expression For Duplicate Words
Date: Thu, 3 Feb 2022 20:48:00 +0100
Message-ID: <20220203194800.pg3bzm33dzzdpqfb@hjp.at> (raw)
In-Reply-To: <CA+i5JwYvfvyvNbyeo=PYvVvWnN08Oeen0Ms3kmXya=JpOKZtjw@mail.gmail.com>
References: <CA+i5JwYvfvyvNbyeo=PYvVvWnN08Oeen0Ms3kmXya=JpOKZtjw@mail.gmail.com>

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[[:>:]]';

        hp

-- 
   _  | Peter J. Holzer    | Story must make more sense than reality.
|_|_) |                    |
| |   | hjp@hjp.at         |    -- Charles Stross, "Creative writing
__/   | http://www.hjp.at/ |       challenge!"

Attachments:

  signature.asc (application/pgp-signature, 832B) download

view thread (5+ messages)  latest in thread

Message-ID: <20220203194800.pg3bzm33dzzdpqfb@hjp.at>
Permalink:  ../20220203194800.pg3bzm33dzzdpqfb@hjp.at/
Also on:    postgresql.org/message-id/20220203194800.pg3bzm33dzzdpqfb@hjp.at

 · 

reply

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: hjp-pgsql@hjp.at
  Subject: Re: Regular Expression For Duplicate Words
  In-Reply-To: <20220203194800.pg3bzm33dzzdpqfb@hjp.at>
  References: <CA+i5JwYvfvyvNbyeo=PYvVvWnN08Oeen0Ms3kmXya=JpOKZtjw@mail.gmail.com> <20220203194800.pg3bzm33dzzdpqfb@hjp.at>

* 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