pg.ddx.io pgsql-general@postgresql.org mailing list archive
help / color / mirror / Atom feedRegular Expression For Duplicate Words
5+ messages / 4 participants
[nested] [flat]
* Regular Expression For Duplicate Words
@ 2022-02-02 08:00 Shaozhong SHI <shishaozhong@gmail.com>
0 siblings, 2 replies; 5+ messages in thread
From: Shaozhong SHI @ 2022-02-02 08:00 UTC (permalink / raw)
To: pgsql-general <pgsql-general@lists.postgresql.org>
This link is interesting.
regex - Regular Expression For Duplicate Words - Stack Overflow
<https://stackoverflow.com/questions/2823016/regular-expression-for-duplicate-words;
Is there any example in Postgres?
Regards,
David
^ permalink raw reply [nested|flat] 5+ messages in thread
* Re: Regular Expression For Duplicate Words
@ 2022-02-02 15:22 David G. Johnston <david.g.johnston@gmail.com>
parent: Shaozhong SHI <shishaozhong@gmail.com>
1 sibling, 1 reply; 5+ messages in thread
From: David G. Johnston @ 2022-02-02 15:22 UTC (permalink / raw)
To: Shaozhong SHI <shishaozhong@gmail.com>; +Cc: pgsql-general <pgsql-general@lists.postgresql.org>
On Wed, Feb 2, 2022 at 1:00 AM Shaozhong SHI <shishaozhong@gmail.com> wrote:
> This link is interesting.
>
> regex - Regular Expression For Duplicate Words - Stack Overflow
> <https://stackoverflow.com/questions/2823016/regular-expression-for-duplicate-words;
>
> Is there any example in Postgres?
>
>
Not that I'm immediately aware of, and I'm not going to search the internet
for you.
The regex capabilities in PostgreSQL are pretty full-featured so a solution
should be possible. You should try translating the SO post concepts into
PostgreSQL yourself and ask specific questions if you get stuck.
David J.
^ permalink raw reply [nested|flat] 5+ messages in thread
* Re: Regular Expression For Duplicate Words
@ 2022-02-02 18:21 Jian He <hejian.mark@gmail.com>
parent: David G. Johnston <david.g.johnston@gmail.com>
0 siblings, 0 replies; 5+ messages in thread
From: Jian He @ 2022-02-02 18:21 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>
It's an interesting question. But I also don't know how to do it in
PostgreSQL.
But I figured out alternative solutions.
GNU Grep: grep -E '(hello)[[:blank:]]+\1' <<<'one hello hello world'
ripgrep: rg '(hello)[[:blank:]]+\1' --pcre2 <<<'one hello hello world'
On Wed, Feb 2, 2022 at 8:53 PM David G. Johnston <david.g.johnston@gmail.com>
wrote:
> On Wed, Feb 2, 2022 at 1:00 AM Shaozhong SHI <shishaozhong@gmail.com>
> wrote:
>
>> This link is interesting.
>>
>> regex - Regular Expression For Duplicate Words - Stack Overflow
>> <https://stackoverflow.com/questions/2823016/regular-expression-for-duplicate-words;
>>
>> Is there any example in Postgres?
>>
>>
> Not that I'm immediately aware of, and I'm not going to search the
> internet for you.
>
> The regex capabilities in PostgreSQL are pretty full-featured so a
> solution should be possible. You should try translating the SO post
> concepts into PostgreSQL yourself and ask specific questions if you get
> stuck.
>
> David J.
>
>
^ permalink raw reply [nested|flat] 5+ messages in thread
* Re: Regular Expression For Duplicate Words
@ 2022-02-03 19:48 Peter J. Holzer <hjp-pgsql@hjp.at>
parent: Shaozhong SHI <shishaozhong@gmail.com>
1 sibling, 1 reply; 5+ messages in thread
From: Peter J. Holzer @ 2022-02-03 19:48 UTC (permalink / raw)
To: pgsql-general@lists.postgresql.org
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
^ permalink raw reply [nested|flat] 5+ messages in thread
* Re: Regular Expression For Duplicate Words
@ 2022-02-03 21:09 Shaozhong SHI <shishaozhong@gmail.com>
parent: Peter J. Holzer <hjp-pgsql@hjp.at>
0 siblings, 0 replies; 5+ messages in thread
From: Shaozhong SHI @ 2022-02-03 21:09 UTC (permalink / raw)
To: pgsql-general <pgsql-general@lists.postgresql.org>
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
^ permalink raw reply [nested|flat] 5+ messages in thread
end of thread, other threads:[~2022-02-03 21:09 UTC | newest]
Thread overview: 5+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2022-02-02 08:00 Regular Expression For Duplicate Words Shaozhong SHI <shishaozhong@gmail.com>
2022-02-02 15:22 ` David G. Johnston <david.g.johnston@gmail.com>
2022-02-02 18:21 ` Jian He <hejian.mark@gmail.com>
2022-02-03 19:48 ` Peter J. Holzer <hjp-pgsql@hjp.at>
2022-02-03 21:09 ` Shaozhong SHI <shishaozhong@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