I'm using the below code from a bash script to replace single quotes from my title column in a postgres database with spaces. psql -U postgres -d my_database -c UPDATE my_table SET title = regexp_replace(title, '''', ' ', 'g')My problem is that I intended to remove the single quotes around strings, for example, like this:this is example 'number one' okbecomesthis is example  number one  ok This works as I had hoped.However, it is also removing the single quotes from within words, so:can'tbecomescan tHow can I specify that I only want to replace the single quotes around strings.
Using regexp_replace to replace single quotes around string but not within words e.g. can't
bash;text processing;replace;postgresql
I don't think there is any nice way to do it with just one replacement. But if four replacements are fine, proceed as follows:Replace \A' by  (that is, replace ' at the beginning of the string).Replace '\Z by  (that is, replace ' at the end of the string).Globally replace ([^[:alnum:]])' by \1  (that is, replace every sequence of a non-alphanumeric character and ' by the non-alphanumeric character and ).Globally replace '([^[:alnum:]]) by  \1 (that is, replace every sequence of ' and a non-alphanumeric character by  and the non-alphanumeric character.)The net effect is to replace every ', except those that are preceded and followed by an alphanumeric character.Disclaimer: I have no clue about PostgreSQL; you have to translate it into PostgreSQL syntax yourself.