Re: Regular expression. How to disable ALL meta-character - Mailing list pgsql-general

From Michael Fuhr
Subject Re: Regular expression. How to disable ALL meta-character
Date
Msg-id 20050422034836.GA45440@winnie.fuhr.org
Whole thread Raw
In response to Re: Regular expression. How to disable ALL meta-character  (David Gagnon <dgagnon@siunik.com>)
List pgsql-general
On Thu, Apr 21, 2005 at 09:03:43AM -0400, David Gagnon wrote:
>
> Just want to share the solution I got to solve my problem. I wanted to
> be eable to search a string (say X) (non case sensitive) without having
> meta-character involved.  The X string come directy from the web so any
> [%]* may cause error in regular expression (because they form non valid
> expression)
>
> 1) Using like: select * from mytable where lower(mycol) LIKE lower("%"
> || lower(X) || "%");
> Mostly perfect solution.  Don't crash but % still have a special
> meaning. Wich means anything

Have you considered using position() or strpos()?  They do simple
substring searches without any metacharacters.

SELECT position(lower('AbC') in lower('aBcDeF'));
 position
----------
        1
(1 row)

SELECT position(lower('xYz') in lower('aBcDeF'));
 position
----------
        0
(1 row)

You might also want to look at the contrib/pg_trgm module to see
if it would be useful.

--
Michael Fuhr
http://www.fuhr.org/~mfuhr/

pgsql-general by date:

Previous
From: Typing80wpm@aol.com
Date:
Subject: listing all tables
Next
From: Michael Fuhr
Date:
Subject: Re: listing all tables