pg.ddx.io  pgsql-general@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Alvaro Herrera <alvherre@alvh.no-ip.org>
To: Shaozhong SHI <shishaozhong@gmail.com>
Cc: pgsql-general <pgsql-general@lists.postgresql.org>
Subject: Re: Match 2 words and more
Date: Sat, 27 Nov 2021 21:49:07 -0300
Message-ID: <202111280049.bmeoep6puysk@alvherre.pgsql> (raw)
In-Reply-To: <CA+i5Jwbzej+34xLZvyVj1rM=SrXO1f+P4scaJUA2Y4xQfAJ0=A@mail.gmail.com>

On 2021-Nov-28, Shaozhong SHI wrote:

> this is supposed to find those to have 2 words and more.
> 
> select name FROM a_table where "STREET_NAME" ~ '^[[:alpha:]+ ]+[:alpha:]+$';
> 
> But, it finds only one word as well.

How about something like this?

'^([[:<:]][[:alpha:]]+[[:>:]]( |$)){2}$'

You have:
- the ^ is a constraint that matches start of string
- you have a ( ... ){2}$ construct which means "match exactly twice" and
  then match end-of-string
- Inside the parens of that construct, you match:
  - [[:<:]] which means start-of-word
  - [[:alpha:]]+ which means "a non-empty set of alphabetical chars"
  - [[:>:]] which means end-of-word
  - ( |$) for "either a space or end-of-string"

You can perhaps simplify by removing the [[:<:]] and [[:>:]]
constraints, so '^([[:alpha:]]+( |$)){2}$'

To mean "between two and four", change the {2} to {2,4}.  If you want
"two or more", try {2,}.

You could change the ( |$) to ([[:white:]]+|$) in order to accept more
than one space between words, or combinations of space and tabs and
newlines and so on.

With a decent set of data, you could probably notice some other problems
in this regexp, but at least it should be a decent start.

> It appears that regex is not robust.

Nah.

-- 
Álvaro Herrera           39°49'30"S 73°17'W  —  https://www.EnterpriseDB.com/





view thread (9+ messages)  latest in thread

Message-ID: <202111280049.bmeoep6puysk@alvherre.pgsql>
Permalink:  ../202111280049.bmeoep6puysk@alvherre.pgsql/
Also on:    postgresql.org/message-id/202111280049.bmeoep6puysk@alvherre.pgsql

 · 

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: alvherre@alvh.no-ip.org, shishaozhong@gmail.com
  Subject: Re: Match 2 words and more
  In-Reply-To: <202111280049.bmeoep6puysk@alvherre.pgsql>
  References: <CA+i5Jwbzej+34xLZvyVj1rM=SrXO1f+P4scaJUA2Y4xQfAJ0=A@mail.gmail.com> <202111280049.bmeoep6puysk@alvherre.pgsql>

* 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