sql - Regex remove all occurrences of multiple characters in a string -
in postgresql want replace characters (;<>)
occurrences in string.
my query:
update table_name set text = regexp_replace(text, '/[(;<>)]+/g', '');
i think regexp wrong. can help me out it?
use much faster translate()
simple case:
update tbl set text = translate(text, '(;<>)', '');
every character in sec parameter has no counterpart in 3rd parameter replaced nothing.
the regular look solution this:
regexp_replace(text, '[(;<>)]', '', 'g');
essential element 4th parameter 'g'
replace "globally" instead of first match. sec parameter character class. on right track, matter of syntax regexp_replace().
update
if don't expect all rows changed, advise adapt update
statement:
update tbl set text = translate(text, '(;<>)', '') text <> translate(text, '(;<>)', '');
this way avoid (expensive) empty updates. (null
covered automatically in particular case.)
sql regex postgresql pattern-matching
No comments:
Post a Comment