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]+?.

Sign up to request clarification or add additional context in comments.

Comments

8

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

Comments

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.

Comments

4

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');

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.