Re: regexp_replace
От
David G. Johnston
Тема
Re: regexp_replace
Дата
Msg-id
CAKFQuwb6Ev2nbB=V-162tdaZ5hVB5d6DmYLYiFaqdV8xj8AW7A@mail.gmail.com
Ответ на
regexp_replace (Andy Colson)
Список
Дерево обсуждения
regexp_replace Andy Colson <andy@squeakycode.net>
Re: regexp_replace Tom Lane <tgl@sss.pgh.pa.us>
Re: regexp_replace Andy Colson <andy@squeakycode.net>
Re: regexp_replace John McKown <john.archie.mckown@gmail.com>
Re: regexp_replace Andy Colson <andy@squeakycode.net>
Re: regexp_replace "David G. Johnston" <david.g.johnston@gmail.com>
Re: regexp_replace Andy Colson <andy@squeakycode.net>
Re: regexp_replace "David G. Johnston" <david.g.johnston@gmail.com>
On Thu, Jan 14, 2016 at 12:43 PM, Andy Colson <andy@squeakycode.net> wrote:
Hi all.
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)
Solution: select regexp_replace('71.09.6.01.3', '(\d)[.-](?=\d)', '\1\2', 'g');
Reason: in the original the trailing "(\d)" eats the digit following the symbol and then that digit is no longer available for matching the preceding digit in the expression. IOW the same character cannot be used to match both the first \d and the second \d so once the first \d captures the 6 there is no \d to match before trailing period.
By using the construct (?:\d) you are zero-width (non-capturing) asserting the the next character is a digit but you are not consuming it and so the continuation of the global matching still has that character to match the first \d.
David J.
В списке pgsql-general по дате отправления