regexp_replace weirdness amounts to a bug?

Started by Erik Rijkersover 2 years ago3 messages
#1Erik Rijkers
er@xs4all.nl

Hello,

The following surprised me enough to think it might be a bug:
(17devel)

select
regexp_replace('Abc Def'
, '([a-z]) ([A-Z])'
, '\1 ' || lower('\2') );

regexp_replace
----------------
Abc Def
(1 row)

-- 'Abc Def' got
-- 'Abc def' expected

What do you think?

Thanks,

Erik Rijkers

#2Malthe
mborch@gmail.com
In reply to: Erik Rijkers (#1)
Re: regexp_replace weirdness amounts to a bug?

Hi Erik,

The regexp doesn't match your string because you're not allowing for
any repeat characters, try adding a '+'.

Show quoted text

On Wed, 16 Aug 2023 at 09:07, Erik Rijkers <er@xs4all.nl> wrote:

Hello,

The following surprised me enough to think it might be a bug:
(17devel)

select
regexp_replace('Abc Def'
, '([a-z]) ([A-Z])'
, '\1 ' || lower('\2') );

regexp_replace
----------------
Abc Def
(1 row)

-- 'Abc Def' got
-- 'Abc def' expected

What do you think?

Thanks,

Erik Rijkers

#3Alvaro Herrera
alvherre@alvh.no-ip.org
In reply to: Erik Rijkers (#1)
Re: regexp_replace weirdness amounts to a bug?

On 2023-Aug-16, Erik Rijkers wrote:

Hello,

The following surprised me enough to think it might be a bug:
(17devel)

select
regexp_replace('Abc Def'
, '([a-z]) ([A-Z])'
, '\1 ' || lower('\2') );

regexp_replace
----------------
Abc Def

What's happening here is that the lower() is applying to the literal \2,
and the expansion of \2 to 'D' occurs afterwards, when lower() has
already executed. Note this other example, where the literal part of
the replacement string is correctly lowercased:

select
regexp_replace('Abc Def'
, '([a-z]) ([A-Z])'
, '\1 ' || lower('D\2D'));
regexp_replace
────────────────
Abc dDdef
(1 fila)

I don't know how to achieve what you want.

--
Álvaro Herrera Breisgau, Deutschland — https://www.EnterpriseDB.com/
#error "Operator lives in the wrong universe"
("Use of cookies in real-time system development", M. Gleixner, M. Mc Guire)