仅使用单引号的正则表达式函数

时间:2015-03-05 09:02:20

标签: oracle

如果列只有一个单引号,我想用正则表达式选择。

实施例.. 第1栏:谁对此负责

第2栏:谁对此负责

使用此查询  从ex_tab中选择regexp_substr(column1,'''')

总是会认为colum2有一个单引号,但实际上它有2个 我想只选择一个单引号而不是双打 我不能使用instr函数,因为我可能有谁负责

2 个答案:

答案 0 :(得分:1)

嗯,你当然可以用INSTR来做这件事:

with str as (select 'who''s in charge?' col1 from dual union all
             select 'who''''s in charge?' col1 from dual union all
             select 'who''s in charge? I''m in charge!' col1 from dual union all
             select 'who is in charge?' col1 from dual union all
             select 'who''''s in charge? I''''m in charge!' from dual)
select col1,
       case when instr(col1, '''''', 1) != 0 then 'no' else 'yes' end is_ok
from   str;

COL1                              IS_OK
--------------------------------- -----
who's in charge?                  yes  
who''s in charge?                 no   
who's in charge? I'm in charge!   yes  
who is in charge?                 yes  
who''s in charge? I''m in charge! no  

它最有可能比正则表达式更快。<​​/ p>

答案 1 :(得分:0)

您可以使用[^]取消集合中的字符。 Some manual。我只是注意到那里没有信息^符号作为方括号中的第一个字符意味着&#34;不在这个集合中#34;。

所以regexp就像:

 ^('?([^']+'[^']+'?)?)*$

应该匹配一个接一个没有两个引号的所有内容。并匹配单引号。并匹配具有多个引号的字符串,由另一个字符串分隔。

所以,把它作为完整的例子,你想要的是:

Select 
    column1,
    REGEXP_COUNT(column1,'^(''?([^'']+''[^'']+''?)?)*$')
from 
(
    select 'who''''s responsible of this' as column1 FROM dual
union
    select 'who''s responsible of this' as column1 FROM dual
union
    select 'who''s responsible ''of'' this' as column1 FROM dual
union
    select '''who''s responsible ''of'' this' as column1 FROM dual
union
    select '''' as column1 FROM dual
)

其中is_ok列表示该值是否包含两个引号。

修改: 我才意识到有更简单的解决方案。只需检查是否有两个或更多的引号一个接一个地出现。所以,正则表达式只是:

'{2,}

这是工作示例,请注意将列名称更改为 is_not_ok

Select 
    column1,
    REGEXP_COUNT(column1,'''{2,}') as is_not_ok
from 
(
    select 'who''''s responsible of this' as column1 FROM dual
union
    select 'who''s responsible of this' as column1 FROM dual
union
    select 'who''s responsible ''of'' this' as column1 FROM dual
union
    select '''who''s responsible ''of'' this' as column1 FROM dual
union
    select '''' as column1 FROM dual
union
    select 'this''' as column1 FROM dual
union
    select '''''' as column1 FROM dual
)