Re: Negative lookbehind assertions in regexs

From: Dawid Kuroczko <qnex42(at)gmail(dot)com>
To: Julian Scarfe <julian(at)avbrief(dot)com>
Cc: pgsql-sql(at)postgresql(dot)org
Subject: Re: Negative lookbehind assertions in regexs
Date: 2005-09-03 17:18:51
Message-ID: 758d5e7f050903101842197d38@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-sql

On 9/3/05, Julian Scarfe <julian(at)avbrief(dot)com> wrote:
>
> >> I'd like a regex that matches 'CD' but not 'ABCD' in any part of the
> >> regex.
>
> From: "Bruno Wolff III" <bruno(at)wolff(dot)to>
>
> > Something like:
> > (^.?CD)|([^B]CD)|([^A]BCD)
>
> Thanks to Bruno, and to Dawid who replied offline. The above does the job
> nicely.

I intended to post Cc: to the list, but somehow I didn't (blame it on
computers,
I just assumed Cc is set ;)).

Anyway, when perl_re's are craved for, once could use PLperl for this:

CREATE OR REPLACE FUNCTION perl_re(v text, r text)
RETURNS boolean LANGUAGE plperl STRICT IMMUTABLE AS $$
my ($val, $re) = @_;
return ($val =~ m{$re}) ? 't' : 'f';
$$;

...though it should be noted that queries WHERE perl_re(col, '(?<!AB)CD')
will not use indexes. (unless there are functional indexes on that function,
but then you would need one index for each regex used).

Regards,
Dawid

In response to

Browse pgsql-sql by date

  From Date Subject
Next Message Robert D. Kennedy 2005-09-03 18:50:02 Table Constraint CHECK(SELECT()) alternative?
Previous Message Alvaro Herrera 2005-09-03 15:28:11 Re: Negative lookbehind assertions in regexs