dose regexp_replace can not use multi regexp(|) in postgresql? Otherwise what should I do?

select regexp_replace('"Tho"mas""', '"$|^"', '');

got Tho"mas"", but I want Tho"mas"



however...

select regexp_split_to_array('"Tho"mas""', '"$|^"')

got {"","Tho\"mas",""}, it is not so bad. then why these two regexp works diffrent?

1 answer

  • answered 2020-08-02 02:17 sticky bit

    Use the global (g) modifier, so that all matches not just the first match are processed:

    SELECT regexp_replace('"Tho"mas""', '"$|^"', '', 'g');
    

    regexp_split_to_array() already matches globally by its nature since it's supposed to split the entire string.