loic_midy
loic_midy

Reputation: 103

regular expression POSIX problem on postgresql

I want to delete all 2 letter words in a string with several words.

I came up with this solution :

SELECT regexp_replace('UN DE DA ','\s{1}[A-Z]{2}\s{1}',' ','g');

SELECT regexp_replace('UN DE DA ','^[A-Z]{2}\s{1}',' ','g');

SELECT regexp_replace('UN DE DA','[A-Z]{2}$',' ','g');

BUT i would like to have on one regexp_replace instead of three I tested this

SELECT regexp_replace('UN DE DA ','\s{1}[A-Z]{2}\s{1}|^[A-Z]{2}\s{1}|[A-Z]{2}$',' ','g');

but it doesn't work => i still have DE

Your sincerely Loïc

Upvotes: 2

Views: 206

Answers (1)

Tim Biegeleisen
Tim Biegeleisen

Reputation: 521389

Assuming you actually want to remove the two letter words, then it seems that your replacement should be empty string, not a single space. We can try matching on the pattern:

\y[A-Z]{2}\y

This will match any two uppercase letters surrounded by word boundaries, which include spaces, or the start/end of the string.

SELECT REGEXP_REPLACE('UN DE DA ', '\y[A-Z]{2}\y', '', 'g');

Demo

The above query returns three spaces, which are left behind from the original string.

Upvotes: 1

Related Questions