Re: Regular expression. How to disable ALL meta-character in a regular expression

From: Michael Fuhr <mike(at)fuhr(dot)org>
To: David Gagnon <dgagnon(at)siunik(dot)com>
Cc: pgsql-general(at)postgresql(dot)org
Subject: Re: Regular expression. How to disable ALL meta-character in a regular expression
Date: 2005-04-20 16:59:29
Message-ID: 20050420165928.GA76076@winnie.fuhr.org
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

On Wed, Apr 20, 2005 at 11:28:28AM -0400, David Gagnon wrote:
>
> I have a web interface with offers a search field. This search field
> will look for the string X in 12 different columns. If the string is
> found anywhere I return the row.
>
> The problem is that the user is eable to put spacial character like : [*
> This create invalid regular expression and make my sql crash.
> ICNUM~* #descriptionOrKeyword#
>
> Is there a way to disable all meta-character.

Why are you doing a regular expression search if you don't want to
allow regular expressions?

> I found this in the manual .. but haven't found example :-(:
> : ....with ***=, the rest of the RE is taken to be a literal string,
> with all characters considered ordinary characters.

Read again the entire sentence, especially the first few words:

If an RE begins with ***=, the rest of the RE is taken to be a
literal string, with all characters considered ordinary characters.

Here are some examples:

SELECT 'test string' ~ 'test[*';
ERROR: invalid regular expression: brackets [] not balanced

SELECT 'test string' ~ '***=test[*';
?column?
----------
f
(1 row)

SELECT 'test[* string' ~ '***=test[*';
?column?
----------
t
(1 row)

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

In response to

Responses

Browse pgsql-general by date

  From Date Subject
Next Message Aaron Bingham 2005-04-20 17:24:08 Strange interaction of union and expressions
Previous Message Scott Marlowe 2005-04-20 16:29:21 Re: Filesystem options for storing pg_data