Discussion:
regexp_replace grief
(too old to reply)
Armin Resch
2013-04-10 23:59:29 UTC
Permalink
Not sure this is the right list to vent about this but here you go:

I) select regexp_replace('BEFORE.AFTER','(.*)\..*','\1','g') "Substring"
II) select regexp_replace('BEFORE.AFTER','(.*)\\..*','\\1','g') "Substring"

Executing (II) against pg 8.4.4 or 9.0.4 yields 'BEFORE', but in order for
9.1.7 to yield the same one has to execute (I) .. bummer

-ar
Craig James
2013-04-11 01:08:51 UTC
Permalink
Post by Armin Resch
I) select regexp_replace('BEFORE.AFTER','(.*)\..*','\1','g') "Substring"
II) select regexp_replace('BEFORE.AFTER','(.*)\\..*','\\1','g') "Substring"
Executing (II) against pg 8.4.4 or 9.0.4 yields 'BEFORE', but in order for
9.1.7 to yield the same one has to execute (I) .. bummer
This has nothing to do with regexp's. It's a change in how '\' is
interpreted in any quoted string. The change came with Postgres 9.x and is
documented in the release notes. It brings Postgres into compliance with
the SQL standard.

In Perl, I do something like this:

my $pg_bs_char;
if ($dbh->get_info($GetInfoType{SQL_DBMS_VER}) gt "09.00.00") {
$pg_bs_char = "\\"; # a single '\' for PG 9.1 and higher
} else {
$pg_bs_char = "\\\\"; # a double '\\' for PG up to 9.0
}

You can also revert to the old 8.x interpretation; see

http://www.postgresql.org/docs/9.1/static/runtime-config-compatible.html

Craig
Post by Armin Resch
-ar
Armin Resch
2013-04-11 01:22:13 UTC
Permalink
Thx for clarification, Craig. Your Perl snippet comes in handy, too.
-ar
Post by Armin Resch
I) select regexp_replace('BEFORE.AFTER','(.*)\..*','\1','g') "Substring"
II) select regexp_replace('BEFORE.AFTER','(.*)\\..*','\\1','g') "Substring"
Executing (II) against pg 8.4.4 or 9.0.4 yields 'BEFORE', but in order for 9.1.7 to yield the same one has to execute (I) .. bummer
This has nothing to do with regexp's. It's a change in how '\' is interpreted in any quoted string. The change came with Postgres 9.x and is documented in the release notes. It brings Postgres into compliance with the SQL standard.
my $pg_bs_char;
if ($dbh->get_info($GetInfoType{SQL_DBMS_VER}) gt "09.00.00") {
$pg_bs_char = "\\"; # a single '\' for PG 9.1 and higher
} else {
$pg_bs_char = "\\\\"; # a double '\\' for PG up to 9.0
}
You can also revert to the old 8.x interpretation; see
http://www.postgresql.org/docs/9.1/static/runtime-config-compatible.html
Craig
Post by Armin Resch
-ar
Loading...