Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to remove duplicates from comma separated list by regex in Oracle regexp_replace? [duplicate]

Tags:

regex

oracle

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...

like image 955
Boris Mitioglov Avatar asked Dec 26 '22 02:12

Boris Mitioglov


2 Answers

Select replace(REGEXP_REPLACE('1234567890,2220006789,2220006789,477-000-6789','([^,]+)(,[ ]*\1)+'),',,',',') 
As [columnName] From [tableName]
like image 37
Pankaj Avatar answered Dec 27 '22 16:12

Pankaj


([^,]+)(,[ ]*\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.

like image 181
vks Avatar answered Dec 27 '22 16:12

vks