简体   繁体   中英

Postgresql regex_replace comma, single and double quotes in a single

I have a string which consists of double quotes, single quotes and commas. I would like to replace all the occurrences of them using regex_replace.

Tried

REGEXP_REPLACE(translate (links, '"',''), '['''''',]'        , '') 

It replaces the first occurrence of comma not the second one.

'https://google.com/khjdbgksdngksd#/","https://google.com/khjdbgksdngksd#/","'

Why are you mixing TRANSLATE and REGEXP_REPLACE? Just pick one and use it, as either one can do all that you want.

If you want REGEXP_REPLACE to replace all instances, you have to give it a fourth argument (the flag argument) of 'g' for 'global', otherwise it stops after the first match and substitution.

Also, to preserve sanity I would use dollar-quoting when the thing being quoted has single quote marks (which yours has in considerable excess).

Using TRANSLATE is probably a better tool for the job, but your title was specifically about REGEXP_REPLACE, so:

REGEXP_REPLACE(links, $$[',"]$$, '', 'g');

Why not just use replace() ?

select replace(replace(replace(links, '"', ''), '''', ''), ',', '')

Or more simply, use translate() :

select translate(links, '"'',', '')

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM