Escaping regular expressions in plperl

Started by Toomas Vendelinalmost 17 years ago2 messagesgeneral
Jump to latest
#1Toomas Vendelin
pg@vendelin.com

How should one escape regular expressions in plperl?

In the following chunk of PlPerl code the date doesn't match the regex:

my $endby = '2009-06-13';

my $t = spi_exec_query(qq|SELECT CASE WHEN '$endby' ~ E'.*\\d\\d\\d\\d
\\-\\d\\d?\\-\\d\\d?.*'
THEN '$endby'::timestamptz ELSE CURRENT_DATE::timestamptz END AS
test|);

die "$t->{rows}[0]{test}";

However, when I run the following query using PgAdmin, the date DOES
match the regex.

SELECT CASE WHEN '$endby' ~ E'.*\\d\\d\\d\\d\\-\\d\\d?\\-\\d\\d?.*'
THEN '$endby'::timestamptz ELSE CURRENT_DATE::timestamptz END AS
test

#2Tom Lane
tgl@sss.pgh.pa.us
In reply to: Toomas Vendelin (#1)
Re: Escaping regular expressions in plperl

Toomas Vendelin <pg@vendelin.com> writes:

In the following chunk of PlPerl code the date doesn't match the regex:

my $endby = '2009-06-13';

my $t = spi_exec_query(qq|SELECT CASE WHEN '$endby' ~ E'.*\\d\\d\\d\\d
\\-\\d\\d?\\-\\d\\d?.*'
THEN '$endby'::timestamptz ELSE CURRENT_DATE::timestamptz END AS
test|);

Backslash is an active escape character in qq constants, no? You'd need
to double all those backslashes, or choose a different quoting method
for the query. (And I hope this is all encased in dollar quoting for
the function body...)

regards, tom lane