What is wrong with the following regexp_replace
select regexp_replace(
'<P><STRONG>Random text.</STRONG></P><IMG src="hello.jpg">',
'(<[/]?[A-Z]+)',
lower('\1'),
'g'
);
I would like it to return
<p><strong>Random text.</strong><p><img src="hello.jpg">
but it returns the input string:
<P><STRONG>Random text.</STRONG></P><IMG src="hello.jpg">
DB fiddle:
What is wrong with the following regexp_replace
select regexp_replace(
'<P><STRONG>Random text.</STRONG></P><IMG src="hello.jpg">',
'(<[/]?[A-Z]+)',
lower('\1'),
'g'
);
I would like it to return
<p><strong>Random text.</strong><p><img src="hello.jpg">
but it returns the input string:
<P><STRONG>Random text.</STRONG></P><IMG src="hello.jpg">
DB fiddle: https://dbfiddle.uk/-VEFpAgH
Share Improve this question asked Mar 28 at 19:09 thebjornthebjorn 27.4k12 gold badges105 silver badges148 bronze badges 7 | Show 2 more comments2 Answers
Reset to default 2Here's an example mirroring regexp_replace()
signature, so you can call it exactly the same way, not just for this particular example.
All it does is inject and evaluate the lower()
call around your replacement string ($3
parameter):
create function regexp_replace_lower(inout text,text,text,text default '')
immutable parallel safe strict as $function$
begin
execute format( 'select $
lower()
call is evaluated beforeregexp_replace()
, so it just folds to a\1
and that's what the function sees. You'd need something like'lower(\1)'
(lower called by the regex engine) for that to work. It sounds like you'd have to extract matches from your input string, calllower()
outside, then replace the matched substring with the lowercased substring. – Zegarek Commented Mar 28 at 19:21'lower(\1)'
produceslower(<P)>lower(<STRONG)>...
– thebjorn Commented Mar 28 at 19:24lower()
called from inside there, but there's no such thing. I guess you could also put together a long and ugly regex emulatinglower()
by conditionally replacingA
witha
,B
withb
and so on :) For ASCII html tags it's just ugly, for anything with accents it'd be hell – Zegarek Commented Mar 28 at 19:29lower(match)
. – Zegarek Commented Mar 28 at 19:32