【问题标题】:Escaping a single quote in Oracle regex query在 Oracle 正则表达式查询中转义单引号
【发布时间】:2013-10-29 06:53:35
【问题描述】:

这真的开始疼了!

我正在尝试使用正则表达式条件在 Oracle 开发人员中编写查询

我的目标是找出所有姓氏中包含的字符通常不包含在姓名中(非字母、空格、连字符和单引号)

即 我需要找到

J00ls
McDonald "Macca"
Smithy (Smith)

找不到

Smith
Mckenzie-Smith
El Hassan
O'Dowd

我现在的查询是

select * from dm_name 
WHERE regexp_like(last_name, '([^A-Za-z -])')
and batch_id = 'ATEST';

它排除了除单引号之外的所有预期内容。在放置单引号字符时,Oracvel SQL Develoepr 解析器将其视为文字。

我试过了:

\' -- but got a "missing right parenthesis" error
||chr(39)|| -- but the search returned nothing
'' -- negated the previous character in the matching group e.g. '([^A-Za-z -''])' made names with '-' return.

如果你能提供任何东西,我将不胜感激。

【问题讨论】:

    标签: sql regex oracle


    【解决方案1】:

    只需将单引号加倍即可转义引号。

    所以

    select *
      from dm_name
     where regexp_like(last_name, '[^A-Za-z ''-]')
       and batch_id = 'ATEST'
    

    另见sqlfiddle。请注意,我在 SQL 开发人员中尝试了类似的查询,效果和小提琴一样好。

    另请注意,要使其工作,- 字符必须是组中的最后一个字符,否则它会尝试查找组 SPACE' 而不是字符 -

    【讨论】:

      【解决方案2】:

      以下作品:

      select * 
        from dm_name 
        WHERE regexp_like(last_name, '([^A-Za-z ''-])');
      

      See this SQLFiddle.

      我无法证明 SQL Developer 是否喜欢它,因为我没有安装该产品。

      分享和享受。

      【讨论】:

        猜你喜欢
        • 2011-05-01
        • 2021-06-04
        • 1970-01-01
        • 2018-10-20
        • 1970-01-01
        • 2013-10-01
        • 2013-05-10
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多