首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >使用带有多个条件的REGEXP_LIKE来匹配模式

使用带有多个条件的REGEXP_LIKE来匹配模式
EN

Stack Overflow用户
提问于 2019-07-08 15:04:22
回答 2查看 58关注 0票数 1

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

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

正在使用REGEXP_LIKE匹配指定的条件,但无法在单个REGEXP_LIKE中包含start with方案:

代码语言:javascript
运行
复制
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
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2019-07-08 15:18:15

这可能是一种简化条件的方法,即使不是在单个regexp中:

代码语言:javascript
运行
复制
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检查前两个字符。

有了你的数据,这个

代码语言:javascript
运行
复制
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

提供:

代码语言:javascript
运行
复制
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                    
票数 0
EN

Stack Overflow用户

发布于 2019-07-08 15:52:06

你说“不能以15开头,2-8,00,000,0000”。

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

或者你可以是正的而不是负的:必须从01到09,或者从10到14,或者从16到19或9n。

有了正条件,你可以得到一个REGEXP:

代码语言:javascript
运行
复制
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
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/56929847

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档