我正在进行下面的查询,我希望使用正则表达式选择精确的7或10位数值列,我在oracle的regexp_like()函数中使用了express,但是它不起作用,请帮忙
查询:
select * from
(select '1234567CELL' "a" from dual
union
select '123CaLLAsasd12' "a" from dual
union
select 'as9960488188CELLas12' "a" from dual
union
select '1234567' "a" from dual
union
select '9960488188' "a" from dual
union
select 'asdCELLqw' "a" from dual) b
where b."a" like '%CELL%' and regexp_like(b."a",'^(\d{7}|\d{10})$');
预期输出
" 1234567"
" 9960488188"
如上两行,请检查
答案 0 :(得分:0)
^
和$
匹配字符串的开头和结尾,值不能包含字符串CELL
,只能是7位或10位数字。相反,您可以使用正则表达式(^|\D)(\d{7}|\d{10})($|\D)
,它将匹配字符串的开头或非数字字符(^|\D)
,然后是7位或10位数字,然后是字符串的结尾或非数字字符($|\D)
。
像这样:
WITH data ( a ) AS (
select '1234567CELL' from dual union
select '123CaLLAsasd12' from dual union
select 'as9960488188CELLas12' from dual union
select '1234567' from dual union
select '9960488188' from dual union
select 'asdCELLqw' from dual
)
SELECT a,
REGEXP_SUBSTR( a, '(^|\D)(\d{7}|\d{10})($|\D)', 1, 1, NULL, 2 ) AS val
FROM data
WHERE a LIKE '%CELL%'
AND REGEXP_LIKE( a, '(^|\D)(\d{7}|\d{10})($|\D)');
<强>输出强>:
A VAL
-------------------- ----------
1234567CELL 1234567
as9960488188CELLas12 9960488188
答案 1 :(得分:0)
您可以使用
where regexp_like(b."a",'^([[:digit:]]{7}|[[:digit:]]{10})$')
由于模式已锚定(^
匹配字符串的开头且$
匹配字符串的结尾),因此在您获取的条目中不能有CELL
,并且您可以从查询中删除where b."a" like '%CELL%'
。