Negative lookbehind assertions in regexs - Mailing list pgsql-sql

From Julian Scarfe
Subject Negative lookbehind assertions in regexs
Date
Msg-id 008901c5ac9b$31bb8b50$0600a8c0@Wilbur
Whole thread Raw
In response to PostgreSQL help  ("Shavonne Marietta Wijesinghe" <shavonne.marietta@studioform.it>)
Responses Re: Negative lookbehind assertions in regexs  (Bruno Wolff III <bruno@wolff.to>)
List 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 




pgsql-sql by date:

Previous
From: PFC
Date:
Subject: Re: PostgreSQL help
Next
From: Bruno Wolff III
Date:
Subject: Re: Negative lookbehind assertions in regexs