precompiling regular expressions in plpgsql - Mailing list pgsql-novice

From Brian Hurt
Subject precompiling regular expressions in plpgsql
Date
Msg-id 44CE615E.5070806@janestcapital.com
Whole thread Raw
Responses Re: precompiling regular expressions in plpgsql  (Tom Lane <tgl@sss.pgh.pa.us>)
List pgsql-novice
Is there a way to precompile regular expressions in plpgsql?

I have a plpgsql function that's doing some fairly heavy duty regular
expression matching, including a lot of calls to substring, as I iterate
over a table.  Worse yet, the regular expressions when written out
completely are fairly complex- except that they are a small number of
common patterns done over and over again.  To make them readable and
writable, I've been splitting them up into variables, so I now have code
like (pardon the typos):

    opt_space = '[[:space:]]*';
    start_of_string = '^';
    end_of_string = opt_space || '$';
    num = opt_space ||
'(([[:digit:]]+(\\.[[:digit:]]+))|(\\.[[:digit:]]+))';


    FOR rec IN SELECT * FROM table
    LOOP

       IF (rec.column ~ (start_of_string || num || opt_space || '%' ||
opt_space || '&'
          || num || end_of_string ))
       THEN
          ...

And much much more in the general pattern.

Now, the strings that I'm using as the pattern as a regular expression
really are constant, even if they don't look like it to postgres.  Is it
possible to precompile these expressions (basically, build up the tables
for the DFA engine ahead of time) in plpgsql?  For various reasons,
switching to another language like perl or python is politically fraught.

Brian


pgsql-novice by date:

Previous
From: Dylan Fogarty-MacDonald
Date:
Subject: Re: Date format
Next
From: Tom Lane
Date:
Subject: Re: precompiling regular expressions in plpgsql