pg.ddx.io  pgsql-sql@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Julian Scarfe <julian@avbrief.com>
To: pgsql-sql@postgresql.org
Subject: Negative lookbehind assertions in regexs
Date: Mon, 29 Aug 2005 14:11:37 +0100
Message-ID: <008901c5ac9b$31bb8b50$0600a8c0@Wilbur> (raw)
References: <001a01c5ac84$496fc750$1102a8c0@dream>

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 





view thread (5+ messages)  latest in thread

Message-ID: <008901c5ac9b$31bb8b50$0600a8c0@Wilbur>
Permalink:  ../008901c5ac9b$31bb8b50$0600a8c0@Wilbur/
Also on:    postgresql.org/message-id/008901c5ac9b$31bb8b50$0600a8c0@Wilbur

 · 

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients using the --to and --cc options:
  reply via email

  To: pgsql-sql@postgresql.org
  Cc: julian@avbrief.com
  Subject: Re: Negative lookbehind assertions in regexs
  In-Reply-To: <008901c5ac9b$31bb8b50$0600a8c0@Wilbur>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox