繁体   English   中英

将REGEXP_LIKE与多重条件配合使用以匹配模式

[英]Using REGEXP_LIKE with mulltiple conditions to match patterns

表名称:测试列名称:ID列ID的正确格式-'^ \\ d {4}-\\ d {6}-\\ d {3}-\\ d1} $'

条件:必须与上述模式匹配,但不能以15开头,数字2-8、00、000、0000。

使用REGEXP_LIKE匹配指定的条件,但无法在单个REGEXP_LIKE中包括场景开始:

with test as (
select  '0614-210297-103-6' ID from dual union all
select  '0014-210297-103-6' ID from dual union all
select  '0004-210297-103-6' ID from dual union all
select  '0000-210297-103-6' ID from dual union all
select  '00120792-2..' ID from dual union all
select  '0614- 210297-103-6' ID from dual union all
select  '0614210297-103-6' ID from dual union all
select  '2614-210297-103-6' ID from dual
)        
select
case
    when regexp_like(ID, '^\d{4}-\d{6}-\d{3}-\d{1}$') 
        then ID
    else
        case
            when regexp_count(ID, '\d') = 14
                then
                    case
                        when
                            not regexp_like(ID,'^15|^2-8|^00|^000|^0000')
                        then ID
                    end
            else ID
        end
end ID_tr
from test

即使不是在单个正则表达式中,这也可能是简化条件的一种方式:

regexp_like(ID, '^[190]\d{3}-\d{6}-\d{3}-\d$') and
substr(ID, 1, 2) not in ('00', '15')

在这里,我使用^[190]\\d{3}而不是^\\d{4}来仅在第一个数字不在2-8时才匹配; 我发现避免以15或00、000、0000开头的字符串的唯一方法是使用substr检查前两个字符。

根据您的数据,这

select ID,
    case
     when regexp_like(ID, '^[190]\d{3}-\d{6}-\d{3}-\d$') and
          substr(ID, 1, 2) not in ('00', '15') then ID
    end as result
from test

给出:

ID                 RESULT            
------------------ ------------------
0614-210297-103-6  0614-210297-103-6 
0014-210297-103-6                    
0004-210297-103-6                    
0000-210297-103-6                    
00120792-2..                         
0614- 210297-103-6                   
0614210297-103-6                     
2614-210297-103-6                    

您说“不得以15开头,数字应为2-8,00,000和0000”。

您可以简化为:不得以00、15、2n到8n开头。

或者,您可以肯定而不是否定:必须以01到09或10到14或16到19或9n开头。

在积极的条件下,您可以获得一个REGEXP:

with test(id) as (
select  '0614-210297-103-6' ID from dual union all
select  '0014-210297-103-6' ID from dual union all
select  '0004-210297-103-6' ID from dual union all
select  '0000-210297-103-6' ID from dual union all
select  '00120792-2..' ID from dual union all
select  '0614- 210297-103-6' ID from dual union all
select  '0614210297-103-6' ID from dual union all
select  '1514-210297-103-6' ID from dual union all
select  '1614-210297-103-6' ID from dual union all
select  '2614-210297-103-6' ID from dual union all
select  '9614-210297-103-6' ID from dual
)      
select id from test
where regexp_like(id,'^(0[1-9]|1[1-46-9]|9\d)\d{2}-\d{6}-\d{3}-\d$')

ID
0614-210297-103-6
1614-210297-103-6
9614-210297-103-6

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM