Escaping regular expressions in plperl

From: Toomas Vendelin <pg(at)vendelin(dot)com>
To: pgsql-general(at)postgresql(dot)org
Subject: Escaping regular expressions in plperl
Date: 2009-06-14 09:20:29
Message-ID: BC0D9817-95DD-43D1-9C7E-1933988B0E34@vendelin.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

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

Responses

Browse pgsql-general by date

  From Date Subject
Next Message Sim Zacks 2009-06-14 11:45:03 citext.so
Previous Message Tom Lane 2009-06-13 14:17:11 Re: accessing anyarray elements