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!

sal
  • 1,199
  • 1
  • 13
  • 31

4 Answers4

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

Andrew Cheong
  • 29,362
  • 15
  • 90
  • 145
8

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

alexius
  • 2,501
  • 20
  • 21
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.

Rob McDonell
  • 1,309
  • 9
  • 15
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');
Community
  • 1
  • 1
sal
  • 1,199
  • 1
  • 13
  • 31