pg.ddx.io  pgsql-sql@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Dawid Kuroczko <qnex42@gmail.com>
To: weigelt@metux.de
Cc: pgsql-sql <pgsql-sql@postgresql.org>
Subject: Re: Finding broken regex'es
Date: Wed, 3 Oct 2007 15:36:41 +0200
Message-ID: <758d5e7f0710030636m63bec244paa6cbaf04e66b3d2@mail.gmail.com> (raw)
In-Reply-To: <20071002202313.GB18506@nibiru.local>
References: <20071002202313.GB18506@nibiru.local>

On 10/2/07, Enrico Weigelt <weigelt@metux.de> 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



view thread (8+ messages)  latest in thread

Message-ID: <758d5e7f0710030636m63bec244paa6cbaf04e66b3d2@mail.gmail.com>
Permalink:  ../758d5e7f0710030636m63bec244paa6cbaf04e66b3d2@mail.gmail.com/
Also on:    postgresql.org/message-id/758d5e7f0710030636m63bec244paa6cbaf04e66b3d2@mail.gmail.com

 · 

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: qnex42@gmail.com, weigelt@metux.de
  Subject: Re: Finding broken regex'es
  In-Reply-To: <758d5e7f0710030636m63bec244paa6cbaf04e66b3d2@mail.gmail.com>

* 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