23

Hi i would like to use regexp_replace (or any other postgres function if reasonable) to replace characters in a string by a '', i.e. erase them.

Using regexp_replace(('def4abcdef4ab','4', '','g') i can replace/erase all occurrences of '4', but i want to also replace all occurences of 'e' and b' in the same process, without using several nested processes like regexp_replace(regexp_replace(regexp_replace())).

i.e. i want to be able to provide a short list of different strings which then should be replace by a unique string.

Any ideas? Thanks a lot in advance!

4 Answers 4

32

The canonical way is to use character classes, like so,

regexp_replace('def4abcdef4ab','[4eb]', '','g')

though @alexius's method can also handle strings.

Not sure if perhaps non-greedily quantifying the expression would make it more efficient, e.g. [4eb]+?.

8

regexp_replace('def4abcdef4ab','4|e|b', '','g')

8

You don't need regular expressions for single character replacement Use the translate function, ie

translate('def4abcdef4ab','4eb', '')

If your strings are multiple characters long, then use replace(). Even nested, this would likely give better performance than regular expressions.

3

ok found out the answer myself (helped by PostgreSQL various clean up of string \ varchar):

select regexp_replace(
E'aśx€ ąsd, dsa w|adwf\n  YZas dw dgaa[aw] asdd',
'[^a-zA-Y0-9 ]',
'',
'g');

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.