Re: Quick Regex Question

From: Alvaro Herrera <alvherre(at)commandprompt(dot)com>
To: Howard Cole <howardnews(at)selestial(dot)com>
Cc: pgsql-general(at)postgresql(dot)org
Subject: Re: Quick Regex Question
Date: 2007-12-20 13:27:05
Message-ID: 20071220132705.GD5309@alvh.no-ip.org
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

Howard Cole wrote:

> Hmm. Still think they are crazy - sometimes the characters are interpreted
> as literals - other times not? Thats crazy in my book!

Yeah. ^, like a lot of other chars, means different things when at the
beggining of a [] (where it means "negate the character class") than
any other position inside the [] (where it means "a literal ^") than
outside [] (where it means "anchor to beginning of string").

> I am trying to match the beginning of a name, so to search for
> 'how' in 'Howard Cole' should match
> 'col' in 'Howard Cole' should match
> 'ole' in 'Howard Cole' should NOT match,
>
> So using ~* '(^| )col' works for me! As would '(^col| col)' etc.

I think you are looking for [[:<:]] which means "beginning of word":

alvherre=# select 'Howard Cole' ~* '[[:<:]]ole';
?column?
----------
f
(1 row)

alvherre=# select 'Howard Cole' ~* '[[:<:]]col';
?column?
----------
t
(1 row)

I use to know the symbol as \< on other regex engines. It is also
known as \m on Postgres. It is not specified by the standard, so be
careful with it. Note double backslash is needed:

alvherre=# select 'Howard Cole' ~* e'\\mcol';
?column?
----------
t
(1 row)

alvherre=# select 'Howard Cole' ~* e'\\mole';
?column?
----------
f
(1 row)

> Just as an aside, is there a function that escapes my search string so that
> any special regex characters are replaced? For example, if I was going to
> search for 'howard.cole' in the search string it would convert to
> 'howard[:.:]cole' or 'howard\.cole' - and then convert that into a postgres
> compatible string!

Hmm, I have no idea about that.

--
Alvaro Herrera http://www.CommandPrompt.com/
PostgreSQL Replication, Consulting, Custom Development, 24x7 support

In response to

Browse pgsql-general by date

  From Date Subject
Next Message Richard Huxton 2007-12-20 13:42:08 Re: foreign key constraint, planner ignore index.
Previous Message Andrew Nesheret 2007-12-20 13:26:55 Re: foreign key constraint, planner ignore index.