Sql 使用REGEXP_LIKE和mulltiple条件匹配模式
表名:测试 列名称:ID 列ID-'^\d{4}-\d{6}-\d{3}-\d1}$'的格式正确 条件: 必须匹配上述模式,但不得以15开头,数字范围为2-8000000000 使用REGEXP_可以匹配指定的条件,但无法在单个REGEXP_中包含start with场景,例如:Sql 使用REGEXP_LIKE和mulltiple条件匹配模式,sql,regex,oracle,Sql,Regex,Oracle,表名:测试 列名称:ID 列ID-'^\d{4}-\d{6}-\d{3}-\d1}$'的格式正确 条件: 必须匹配上述模式,但不得以15开头,数字范围为2-8000000000 使用REGEXP_可以匹配指定的条件,但无法在单个REGEXP_中包含start with场景,例如: with test as ( select '0614-210297-103-6' ID from dual union all select '0014-210297-103-6' ID from dual un
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中:
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
这可能是一种简化条件的方法,即使不是在单个regexp中:
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-80000000开始”
您可以将其简化为:不得以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
你说“不能以15开头,数字从2-80000000开始”
您可以将其简化为:不得以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
仅供参考:
1}
是一个打字错误。您可以使用'^\d{4}-\d{6}-\d{3}-\d$'
,因为{1}
总是多余的。谢谢@WiktorStribiżew。更正了文本。仅供参考:1}
是一个打字错误。您可以使用'^\d{4}-\d{6}-\d{3}-\d$'
,因为{1}
总是多余的。谢谢@WiktorStribiżew。更正了文本。