Regex remove all occurrences of multiple characters in a string

无人久伴 提交于 2019-12-08 05:32:03

问题


In my PostgreSQL I want to replace all characters (;<>) occurrences in a string.

My query:

update table_name set text = regexp_replace(text, '/[(;<>)]+/g', '');

I think my regexp is wrong. Can anyone help me out with it?


回答1:


Use the much faster translate() for this simple case:

UPDATE tbl SET text = translate(text, '(;<>)', '');

Every character in the second parameter that has no counterpart in the third parameter is replaced with nothing.

The regular expression solution could look like this:

regexp_replace(text, '[(;<>)]', '', 'g');

Essential element is the 4th parameter 'g' to replace "globally" instead of just the first match. The second parameter is a character class.
You were on the right track, just a matter of syntax for regexp_replace().

Hint on UPDATE

If you don't expect all rows to be changed, I would strongly advise to adapt your UPDATE statement:

UPDATE tbl
SET    text =  translate(text, '(;<>)', '')
WHERE  text <> translate(text, '(;<>)', '');

This way you avoid (expensive) empty updates. (NULL is covered automatically in this particular case.)



来源:https://stackoverflow.com/questions/14953099/regex-remove-all-occurrences-of-multiple-characters-in-a-string

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!