Re: Regular expression character escape

From: Ronan Dunklau <rdunklau(at)gmail(dot)com>
To: Heiko Wundram <modelnine(at)modelnine(dot)org>
Cc: pgsql-general(at)postgresql(dot)org
Subject: Re: Regular expression character escape
Date: 2012-02-24 17:03:18
Message-ID: 4F47C2D6.8000207@gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

On 24/02/2012 17:43, Heiko Wundram wrote:
> Am 24.02.2012 17:40, schrieb Ronan Dunklau:
>> On 24/02/2012 17:09, Heiko Wundram wrote:
>>> Use the corresponding function of your programming language/framework of
>>> choice. E.g. Python delivers this as re.escape().
>>
>> Thank you, but as I wrote in the original post, I don't know how
>> postgresql and python differ in their regexp syntax. Specifically, I
>> know that re.escape escapes any non-alphanumeric character, including
>> accented letters.
>>
>> If you have any evidence proving that everything will work fine with
>> re.escape, I'll be more than happy to use it.
>
> This will work (AFAICT, _and_ as far as I've used it): the regex-syntax
> of Python is a superset of the regex-syntax of PostgreSQL (both are
> based on PCRE, not on the library, but the syntax), and as such you'll
> possibly do "too much" quoting, but never too little when simply using
> the Python-builtin.

Unfortunately for my use case, "too much" quoting can lead to errors in
postgresql.

I just tested it using the following code:

postgres=# create or replace function escape_re(atext varchar) returns
varchar as $$
import re
return re.escape(atext.decode('utf8'))
$$ language plpythonu;

postgres=# select escape_re('testé');
escape_re
-----------
test\é

postgres=# select 'testé' ~ escape_re('testé');
ERROR: invalid regular expression: invalid escape \ sequence

--
Ronan Dunklau

In response to

Responses

Browse pgsql-general by date

  From Date Subject
Next Message Tom Lane 2012-02-24 17:09:38 Re: invalid memory alloc request size 1765277700 Error Question
Previous Message Garrett Murphy 2012-02-24 17:02:32 Re: invalid memory alloc request size 1765277700 Error Question