On 10/2/07, Enrico Weigelt <[EMAIL PROTECTED]> 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 ---------------------------(end of broadcast)--------------------------- TIP 5: don't forget to increase your free space map settings