X-Original-To: pgsql-sql-postgresql.org@localhost.postgresql.org Received: from localhost (av.hub.org [200.46.204.144]) by svr1.postgresql.org (Postfix) with ESMTP id E4940D796C for ; Mon, 29 Aug 2005 13:21:31 -0300 (ADT) Received: from svr1.postgresql.org ([200.46.204.71]) by localhost (av.hub.org [200.46.204.144]) (amavisd-new, port 10024) with ESMTP id 26708-03 for ; Mon, 29 Aug 2005 16:21:28 +0000 (GMT) Received: from wolff.to (wolff.to [66.93.197.194]) by svr1.postgresql.org (Postfix) with SMTP id 3263DD7759 for ; Mon, 29 Aug 2005 13:21:25 -0300 (ADT) Received: (qmail 2781 invoked by uid 500); 29 Aug 2005 16:21:03 -0000 Date: Mon, 29 Aug 2005 11:21:03 -0500 From: Bruno Wolff III To: Julian Scarfe Cc: pgsql-sql@postgresql.org Subject: Re: Negative lookbehind assertions in regexs Message-ID: <20050829162103.GA32443@wolff.to> Mail-Followup-To: Bruno Wolff III , Julian Scarfe , pgsql-sql@postgresql.org References: <001a01c5ac84$496fc750$1102a8c0@dream> <008901c5ac9b$31bb8b50$0600a8c0@Wilbur> Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <008901c5ac9b$31bb8b50$0600a8c0@Wilbur> User-Agent: Mutt/1.5.6i X-Virus-Scanned: by amavisd-new at hub.org X-Spam-Status: No, hits=0.006 required=5 tests=[AWL=0.006] X-Spam-Level: X-Archive-Number: 200508/267 X-Sequence-Number: 22495 On Mon, Aug 29, 2005 at 14:11:37 +0100, Julian Scarfe wrote: > I'd like a regex that matches 'CD' but not 'ABCD' in any part of the regex. > > 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) The above code won't work because there could be both CD and ABCD in the string. What you want to do is match all of the valid possibilities. Something like: (^.?CD)|([^B]CD)|([^A]BCD)