4

I have

'VA - HRD 1, VA - HRD 1, VA - NOVA 1, VA - NOVA 1'

and want to get

'VA - HRD 1, VA - NOVA 1'

I am trying

regexp_replace( 'VA - HRD 1, VA - HRD 1, VA - NOVA 1, VA - NOVA 1' ,'([^,]+)(,\1)+', '\1')

but it doesn't remove all duplicates, it produces: VA - HRD 1, VA - HRD 1, VA - NOVA 1

Please help...

1
  • Assuming duplicates are grouped together ? Commented Oct 31, 2014 at 10:26

2 Answers 2

9
([^,]+)(,[ ]*\1)+

Try this.This works.See demo.

http://regex101.com/r/yG7zB9/8

The issue was VA - HRD 1, VA - HRD 1

                     ^  ^

The space here.You were not taking this into account as the first match has no space behid it.So inlcde [ ]* or \s* to make it accept.

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

11 Comments

Dude, what to do if string without spaces like : National,Carrier,Retail,Internal,National ?
@goodspeed we have used [ ]* * is for 0 t more.So it should work fine
and how to make it work for strings without spaces near comma?
@goodspeed National,Carrier,Retail,Internal,National here duplicates are not grouped together.So a new regex would be needed for this :)
i really dont know why this regex gets reposted all the time, it is wrong as it also matches '3,3' in '3773,3774,3830,3830'
|
0
Select replace(REGEXP_REPLACE('1234567890,2220006789,2220006789,477-000-6789','([^,]+)(,[ ]*\1)+'),',,',',') 
As [columnName] From [tableName]

3 Comments

This answer wold be much improved if it included some explanation of what the code was doing. Whilst it may be correct "code only" doesn't necessarily help supplicants to achieve understanding.
This dosen't answer question he want to make a distinct list not actually remove duplicates..eg A, A, B, B -> A,B correct , A, B , B, C -> A, C incorrect answer .
This could be useful for something else but it does not answer the question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.