pg.ddx.io pgsql-general@postgresql.org mailing list archive
help / color / mirror / Atom feedreplace single char for string using regexp_replace
3+ messages / 3 participants
[nested] [flat]
* replace single char for string using regexp_replace
@ 2019-12-30 00:00 PegoraroF10 <marcos@f10.com.br>
0 siblings, 2 replies; 3+ messages in thread
From: PegoraroF10 @ 2019-12-30 00:00 UTC (permalink / raw)
To: pgsql-general
I have to replace ; , " and ' chars in an string putting its correspondent
ascii value
I have a
Replace(Replace(Replace(Replace($$Text,with;On'It"$$,',','chr(59)'),';','chr(44)'),'"','chr(34)'),'''','chr(39)')
It works but I would like to call just one Replace.
I tried this but didn´t work
select
regexp_replace($$Text,with;On'It"$$,$$,|;|"|'$$,$$chr(59)|chr(44)|chr(34)|chr(39)$$,'g');
, -> chr(59)
; -> chr(44)
" -> chr(34)
' -> chr (39)
--
Sent from: https://www.postgresql-archive.org/PostgreSQL-general-f1843780.html
^ permalink raw reply [nested|flat] 3+ messages in thread
* Re: replace single char for string using regexp_replace
@ 2019-12-30 04:50 Tom Lane <tgl@sss.pgh.pa.us>
parent: PegoraroF10 <marcos@f10.com.br>
1 sibling, 0 replies; 3+ messages in thread
From: Tom Lane @ 2019-12-30 04:50 UTC (permalink / raw)
To: PegoraroF10 <marcos@f10.com.br>; +Cc: pgsql-general
PegoraroF10 <marcos@f10.com.br> writes:
> I have to replace ; , " and ' chars in an string putting its correspondent
> ascii value
> I have a
> Replace(Replace(Replace(Replace($$Text,with;On'It"$$,',','chr(59)'),';','chr(44)'),'"','chr(34)'),'''','chr(39)')
> It works but I would like to call just one Replace.
Use translate(), perhaps?
https://www.postgresql.org/docs/current/functions-string.html
regards, tom lane
^ permalink raw reply [nested|flat] 3+ messages in thread
* Re: replace single char for string using regexp_replace
@ 2019-12-30 13:29 Daniel Verite <daniel@manitou-mail.org>
parent: PegoraroF10 <marcos@f10.com.br>
1 sibling, 0 replies; 3+ messages in thread
From: Daniel Verite @ 2019-12-30 13:29 UTC (permalink / raw)
To: PegoraroF10 <marcos@f10.com.br>; +Cc: pgsql-general
PegoraroF10 wrote:
> I have a
> Replace(Replace(Replace(Replace($$Text,with;On'It"$$,',','chr(59)'),';','chr(44)'),'"','chr(34)'),'''','chr(39)')
> It works but I would like to call just one Replace.
For performance, it might be the fastest method, despite the
lack of elegance and the multiple passes on the string.
In a recent discussion on the list [1] I've asked on how best to do
multiple string replacements. That's a generalized version of your
question and the answer might be heavier but anyway here's some
code on the wiki with plperl and plpgsql versions:
https://wiki.postgresql.org/wiki/Multi_Replace_Perl
https://wiki.postgresql.org/wiki/Multi_Replace_plpgsql
If you're okay with plperl see the first version, as it's
much more efficient, with Perl implementing the multiple
replacement natively in its regexp engine (plus implementing
the search with a trie since 5.10).
The plpgsql version tries do its best with regexp_replace, but it
scales much worse as the number of replacements grows.
But it does work in the situations where a stack of nested replace() calls
wouldn't work, like replace foo with bar and bar with foo.
In your case, as the strings to replace always consist only of one character,
you might also split the string by characters, replace them with a
CASE WHEN... construct, and reassemble the result with string_agg,
as in:
select string_agg(c, '') from
(select case c
when ',' then 'chr(59)'
when ';' then 'chr(44)'
... other substitutions...
else c
end
from
regexp_split_to_table($$The Original String$$, '') as s1(c)
) as s2(c);
If the strings are not too large and there are many occurrences of the
characters
to replace, I would expect this to be more efficient than the more generic
plpgsql-based solution above. Against the Perl version I don't know.
There is a per-call overhead with plperl that can't be ignored if you're
focused on performance.
[1]
https://www.postgresql.org/message-id/306b726b-f185-4668-bffe-ac8e7f78878e@manitou-mail.org
Best regards,
--
Daniel Vérité
PostgreSQL-powered mailer: http://www.manitou-mail.org
Twitter: @DanielVerite
^ permalink raw reply [nested|flat] 3+ messages in thread
end of thread, other threads:[~2019-12-30 13:29 UTC | newest]
Thread overview: 3+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2019-12-30 00:00 replace single char for string using regexp_replace PegoraroF10 <marcos@f10.com.br>
2019-12-30 04:50 ` Tom Lane <tgl@sss.pgh.pa.us>
2019-12-30 13:29 ` Daniel Verite <daniel@manitou-mail.org>
This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox