如何使用 REGEXP_REPLACE 从每一端删除重复的逗号和 trim?
How to remove repeated commas and trim from each end using REGEXP_REPLACE?
我需要将多个字段连接在一起,这些字段可能为 null,也可能不为 null。我可能会得到一个类似这样的字符串:',c,e,' 我实际上想将其显示为 'c,e'.
我可以通过组合 regexp_replace
和 trim
:
with sd as (select 'a,b,c' str from dual union all
select 'a' str from dual union all
select null str from dual union all
select 'a,,,d' from dual union all
select 'a,,,,e,f,,'from dual union all
select ',,,d,,f,g,,'from dual)
select str,
regexp_replace(str, '(,)+', '') new_str,
trim(both ',' from regexp_replace(str, '(,)+', '')) trimmed_new_str
from sd;
STR NEW_STR TRIMMED_NEW_STR
----------- ----------- ---------------
a,b,c a,b,c a,b,c
a a a
a,,,d a,d a,d
a,,,,e,f,, a,e,f, a,e,f
,,,d,,f,g,, ,d,f,g, d,f,g
但我觉得它应该一次就可以完成 regexp_replace
只是我一辈子都想不出来怎么办!
可能吗?如果可以,怎么做?
试一试:
with sd as (select 'a,b,c' str union all
select 'a' union all
select null union all
select 'a,,,d' union all
select 'a,,,,,e,f,,' union all
select ',,,d,,f,g,,')
select str,
regexp_replace(str, '(,){2,}', '', 1, 0) new_str,
trim(both ',' from regexp_replace(str, '(,){2,}', '', 1, 0)) trimmed_new_str
from sd;
查询:
with sd as (select 'a,b,c' str from dual union all
select 'a' from dual union all
select null from dual union all
select 'a,,,d,' from dual union all
select ',a,,,d' from dual union all
select ',a,,,d,' from dual union all
select ',,,a,,,d,,,' from dual union all
select ',a,,,,,e,f,,' from dual union all
select ',,d,,f,g,,' from dual )
select str,
regexp_replace(str, '^,+|,+$|,+(,\w)','') new_str
from sd;
结果:
str new_str
-----------------------
a,b,c a,b,c
a a
(null) (null)
a,,,d, a,d
,a,,,d a,d
,a,,,d, a,d
,,,a,,,d,,, a,d
,a,,,,,e,f,, a,e,f
,,d,,f,g,, d,f,g
模式:
^,+ matches commas at the beginning
| OR
,+$ matches commas at the end
| OR
,+(,\w) matches several commas followed by a single comma and a word.
仅将上面的第一个子表达式替换为逗号和单词。
我需要将多个字段连接在一起,这些字段可能为 null,也可能不为 null。我可能会得到一个类似这样的字符串:',c,e,' 我实际上想将其显示为 'c,e'.
我可以通过组合 regexp_replace
和 trim
:
with sd as (select 'a,b,c' str from dual union all
select 'a' str from dual union all
select null str from dual union all
select 'a,,,d' from dual union all
select 'a,,,,e,f,,'from dual union all
select ',,,d,,f,g,,'from dual)
select str,
regexp_replace(str, '(,)+', '') new_str,
trim(both ',' from regexp_replace(str, '(,)+', '')) trimmed_new_str
from sd;
STR NEW_STR TRIMMED_NEW_STR
----------- ----------- ---------------
a,b,c a,b,c a,b,c
a a a
a,,,d a,d a,d
a,,,,e,f,, a,e,f, a,e,f
,,,d,,f,g,, ,d,f,g, d,f,g
但我觉得它应该一次就可以完成 regexp_replace
只是我一辈子都想不出来怎么办!
可能吗?如果可以,怎么做?
试一试:
with sd as (select 'a,b,c' str union all
select 'a' union all
select null union all
select 'a,,,d' union all
select 'a,,,,,e,f,,' union all
select ',,,d,,f,g,,')
select str,
regexp_replace(str, '(,){2,}', '', 1, 0) new_str,
trim(both ',' from regexp_replace(str, '(,){2,}', '', 1, 0)) trimmed_new_str
from sd;
查询:
with sd as (select 'a,b,c' str from dual union all
select 'a' from dual union all
select null from dual union all
select 'a,,,d,' from dual union all
select ',a,,,d' from dual union all
select ',a,,,d,' from dual union all
select ',,,a,,,d,,,' from dual union all
select ',a,,,,,e,f,,' from dual union all
select ',,d,,f,g,,' from dual )
select str,
regexp_replace(str, '^,+|,+$|,+(,\w)','') new_str
from sd;
结果:
str new_str
-----------------------
a,b,c a,b,c
a a
(null) (null)
a,,,d, a,d
,a,,,d a,d
,a,,,d, a,d
,,,a,,,d,,, a,d
,a,,,,,e,f,, a,e,f
,,d,,f,g,, d,f,g
模式:
^,+ matches commas at the beginning
| OR
,+$ matches commas at the end
| OR
,+(,\w) matches several commas followed by a single comma and a word.
仅将上面的第一个子表达式替换为逗号和单词。