Received: from localhost (unknown [200.46.204.183]) by postgresql.org (Postfix) with ESMTP id 6F0959F9DFB for ; Wed, 3 Oct 2007 10:36:48 -0300 (ADT) Received: from postgresql.org ([200.46.204.71]) by localhost (mx1.hub.org [200.46.204.183]) (amavisd-maia, port 10024) with ESMTP id 59104-04-6 for ; Wed, 3 Oct 2007 10:36:44 -0300 (ADT) X-Greylist: domain auto-whitelisted by SQLgrey-1.7.5 Received: from fk-out-0910.google.com (fk-out-0910.google.com [209.85.128.188]) by postgresql.org (Postfix) with ESMTP id DFF899F9FC3 for ; Wed, 3 Oct 2007 10:36:43 -0300 (ADT) Received: by fk-out-0910.google.com with SMTP id b27so4654757fka for ; Wed, 03 Oct 2007 06:36:41 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=beta; h=domainkey-signature:received:received:message-id:date:from:to:subject:cc:in-reply-to:mime-version:content-type:content-transfer-encoding:content-disposition:references; bh=cZu8NHbbBO4pkCjocWKHUmBrnkEDopdwLHGD/ewaLgA=; b=EqvsIu0vsf2NYjfY4KCZWgmiKGcpP1epiwntMcoJWM+RZsYsKMot1a6LvdrYbb5/TMcU3t3ha/8dc3B33OceimRUg4T3xM7XhH4O9xRKq1/j1sdVwYqRZXdGFoEimEMF30hwZfo9oU6czNB/+246ay3MyHqK/GwM7+xBdX2/gFI= DomainKey-Signature: a=rsa-sha1; c=nofws; d=gmail.com; s=beta; h=received:message-id:date:from:to:subject:cc:in-reply-to:mime-version:content-type:content-transfer-encoding:content-disposition:references; b=WK6K5k3Vrrz+qch6jOHMQZB0BSu2Ff2NVFOKzfiKZB53PUEAjR2Mm3lma+coEmg6DbXJnR7k1Aq9lWfGbP4eunkRNjtclsEjFcEt0NfIPLE10LwCbpTZRKN+GjK2W+vGQm+36ES6e0gDc8GRLw0kdEeUsuKGyIncOBgigY5o2CU= Received: by 10.82.154.12 with SMTP id b12mr14136243bue.1191418601248; Wed, 03 Oct 2007 06:36:41 -0700 (PDT) Received: by 10.82.170.9 with HTTP; Wed, 3 Oct 2007 06:36:41 -0700 (PDT) Message-ID: <758d5e7f0710030636m63bec244paa6cbaf04e66b3d2@mail.gmail.com> Date: Wed, 3 Oct 2007 15:36:41 +0200 From: "Dawid Kuroczko" To: weigelt@metux.de Subject: Re: Finding broken regex'es Cc: pgsql-sql In-Reply-To: <20071002202313.GB18506@nibiru.local> MIME-Version: 1.0 Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: 7bit Content-Disposition: inline References: <20071002202313.GB18506@nibiru.local> X-Virus-Scanned: Maia Mailguard 1.0.1 X-Archive-Number: 200710/10 X-Sequence-Number: 29382 On 10/2/07, Enrico Weigelt wrote: > > Hi folks, > > > I'm looking for some way to find broken regex'es in some column > to kick them off. For now I'm regularily fetching all regexes > from an PHP script, try an preg_match() and so find the broken > ones to later remove them. > > Is there any way to do this directly within the db ? Of course. Exceptions is what You need! CREATE FUNCTION regex_is_broken(r text) RETURNS boolean AS $$ BEGIN PERFORM '' ~ r; RETURN 'f'; EXCEPTION WHEN INVALID_REGULAR_EXPRESSION THEN RETURN 't'; END; $$ LANGUAGE PLpgSQL STRICT IMMUTABLE; ...and then you could do something like: DELETE FROM table WHERE regex_is_broken(rx_col); You don't need PLpgSQL to prevent such invalid regexes in the first place. You could use CHECK constraint for it: CREATE TABLE rx_check ( rx text CHECK ('' ~ rx IN ('t','f')) ); postgres=> INSERT INTO rx_check (rx) VALUES ('.*'); INSERT 0 1 Time: 13.660 ms postgres=> INSERT INTO rx_check (rx) VALUES ('234234'); INSERT 0 1 Time: 2.282 ms postgres=> INSERT INTO rx_check (rx) VALUES ('par).*'); ERROR: invalid regular expression: parentheses () not balanced Regards, Dawid