Re: regexp_replace

From: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
To: Andy Colson <andy(at)squeakycode(dot)net>
Cc: pgsql <pgsql-general(at)postgresql(dot)org>
Subject: Re: regexp_replace
Date: 2016-01-14 19:59:03
Message-ID: 14427.1452801543@sss.pgh.pa.us
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

Andy Colson <andy(at)squeakycode(dot)net> writes:
> This is not doing as I'd expected:

> select regexp_replace('71.09.6.01.3', '(\d)[.-](\d)', '\1\2', 'g');

> regexp_replace
> ----------------
> 71096.013
> (1 row)

I think regexp_replace considers only non-overlapping substrings,
eg, once it's replaced 1.0 with 10, it then picks up searching at
the 9 rather than starting over. The dot after 6 doesn't get
removed because the 6 can't belong to two replaceable substrings, and
it already got consumed in the process of removing the dot before 6.

I might be wrong, but I think two passes of regexp_replace would
do what you want in this example.

regards, tom lane

In response to

Responses

Browse pgsql-general by date

  From Date Subject
Next Message John McKown 2016-01-14 20:02:21 Re: regexp_replace
Previous Message Andy Colson 2016-01-14 19:43:27 regexp_replace