2

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

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.