0

I want to remove duplicate from a string separated with comma with regexp_replace function in Postgres

Input string: emarian, eiulian, evasile, emarina,emarian, eiulian, evasile, emarina

Desired output: emarian, eiulian, evasile, emarina

Please can someone help me!

Desired string:emarian, eiulian, evasile, emarina

Sincerely, I'm not familiar cu regexp_replace

1 Answer 1

2

You can use combination of array functions and distinct to achieve this

Eg query

SELECT array_to_string(array_agg(DISTINCT word), ', ')
FROM regexp_split_to_table('emarian, eiulian, evasile, emarina, emarian, eiulian, evasile, emarina', ', ') AS word;

The above query splits the string by the delimititer to value list and performs distinct and then converts back to string.

I would not recommend regex_replace for this.

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

4 Comments

Yes, but using this function can't help. I need to use regex, because my tool permit me only this
What tool allows you to use regexp_replace but prevents using regexp_split_to_table and other PostgreSQL built-in functions?
I'm using Helix Dashboards
Please update the post with the actual code where you are trying to use regexp_replace. I don't know if PostgreSQL's regex implementation supports all of the features needed to perform the requested operation, but even if it oes, the expression will most certainly be difficult to read (which means it will also be difficult to maintain when changes are needed). What regular expression can remove duplicate items from a string? might prove helpful.

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.