如何通过 Oracle regexp_replace 中的正则表达式从逗号分隔列表中删除重复项?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/26672269/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to remove duplicates from comma separated list by regex in Oracle regexp_replace?
提问by Boris Mitioglov
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' ,'([^,]+)(,)+', '')
but it doesn't remove all duplicates, it produces: VA - HRD 1, VA - HRD 1, VA - NOVA 1
但它不会删除所有重复项,它会产生: VA - HRD 1, VA - HRD 1, VA - NOVA 1
Please help...
请帮忙...
回答by vks
([^,]+)(,[ ]*)+
Try this.This works.See demo.
试试这个。这有效。见演示。
http://regex101.com/r/yG7zB9/8
http://regex101.com/r/yG7zB9/8
The issue was VA - HRD 1, VA - HRD 1
问题是 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.
这里的空间。你没有考虑到这一点,因为第一场比赛没有空间隐藏它。所以 inlcde[ ]*
或\s*
让它接受。
回答by Pankaj
Select replace(REGEXP_REPLACE('1234567890,2220006789,2220006789,477-000-6789','([^,]+)(,[ ]*)+'),',,',',')
As [columnName] From [tableName]