Negative lookbehind assertions in regexs

From: "Julian Scarfe" <julian(at)avbrief(dot)com>
To: <pgsql-sql(at)postgresql(dot)org>
Subject: Negative lookbehind assertions in regexs
Date: 2005-08-29 13:11:37
Message-ID: 008901c5ac9b$31bb8b50$0600a8c0@Wilbur
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-sql

I'd like a regex that matches 'CD' but not 'ABCD' in any part of the regex.

In Perl I'd use a negative lookbehind assertion (?<!AB)CD to do the job:

$ cat lb.pl
print "CD: ", 'CD' =~ /(?<!AB)CD/, "\n";
print "XYCD: ", 'XYCD' =~ /(?<!AB)CD/, "\n";
print "ABCD: ", 'ABCD' =~ /(?<!AB)CD/, "\n";

$ perl lb.pl
CD: 1
XYCD: 1
ABCD:

But Postgresql (7.4) doesn't seem to like that:

# select 'ABCD' ~ '(?<!AB)CD';
ERROR: invalid regular expression: quantifier operand invalid

Is there a workaround that allows me to do this as a single regex?

I know I could and together a ~ and !~ like this

# select ('CD' ~ 'CD') and ('CD' !~ 'ABCD');
?column?
----------
t
(1 row)

# select ('ABCD' ~ 'CD') and ('ABCD' !~ 'ABCD');
?column?
----------
f
(1 row)

but changing the SQL would break the existing paradigm.

TIA

Julian

In response to

Responses

Browse pgsql-sql by date

  From Date Subject
Next Message Bruno Wolff III 2005-08-29 16:21:03 Re: Negative lookbehind assertions in regexs
Previous Message PFC 2005-08-29 11:06:30 Re: PostgreSQL help