Re: Finding broken regex'es - Mailing list pgsql-sql

From Dawid Kuroczko
Subject Re: Finding broken regex'es
Date
Msg-id 758d5e7f0710030636m63bec244paa6cbaf04e66b3d2@mail.gmail.com
Whole thread Raw
In response to Finding broken regex'es  (Enrico Weigelt <weigelt@metux.de>)
Responses Re: Finding broken regex'es
List pgsql-sql
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


pgsql-sql by date:

Previous
From: Tom Lane
Date:
Subject: Re: Finding broken regex'es
Next
From: "Filip Rembiałkowski"
Date:
Subject: Re: Finding broken regex'es