【问题标题】:How to remove repeated commas and trim from each end using REGEXP_REPLACE?如何使用 REGEXP_REPLACE 从每一端删除重复的逗号和修剪?
【发布时间】:2016-05-18 11:50:12
【问题描述】:

我需要将几个字段连接在一起,这些字段可能为空,也可能不为空。我可能最终得到一个字符串,例如:',,c,,e,,' 我实际上想显示为 'c,e'。

我可以通过regexp_replacetrim 的组合来获得这个:

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, '(,)+', '\1') new_str,
       trim(both ',' from regexp_replace(str, '(,)+', '\1')) 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 中是可行的,只是我一生都无法弄清楚如何做到这一点!

有可能吗?如果有,怎么做?

【问题讨论】:

    标签: sql oracle regexp-replace


    【解决方案1】:

    查询:

    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)','\1') 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.
    

    仅将上面的第一个子表达式替换为逗号和单词。

    【讨论】:

    • 完美——一百万年我也想不出这个!即使有你的解释,我也只能掌握它 *{;-) 正则表达式很难,但非常强大。非常感谢!
    • 非常有趣!我正在分解它,试图了解它是如何工作的。当子组包含逗号时,这怎么不返回',d'?:'select regexp_replace(',,,d', '^,+|,+(,\w)', '\1') from dual;'
    • 可能是^,+,+(,\w) 之前的逗号匹配。即使我不完全确定它是如何工作的。我从answer 学到了这项技术。另外,我认为^,+|,+$|(\w,),+ 模式应该可以工作。但是,它不会删除尾随逗号。
    【解决方案2】:

    试一试:

    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', 1, 0) new_str,
           trim(both ',' from regexp_replace(str, '(,){2,}', '\1', 1, 0)) trimmed_new_str
    from   sd;
    

    【讨论】:

    • 不幸的是,这仍然需要 trim() 删除前导/尾随逗号。我正在寻找一个可以做到这一点并删除重复逗号的正则表达式。
    猜你喜欢
    • 2016-10-26
    • 2018-05-30
    • 1970-01-01
    • 1970-01-01
    • 2016-04-29
    • 2021-06-12
    • 2018-04-14
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多