Using postgres regexp_replace to replace a list of strings by always the same string Using postgres regexp_replace to replace a list of strings by always the same string postgresql postgresql

Using postgres regexp_replace to replace a list of strings by always the same string


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


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.


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