为什么这个regexp_like没有得到我想要的结果

Why doesn't this regexp_like does not get the results I want

我正在尝试以下 regexp_like 进行更大的查询,但它不起作用,我做错了什么?

with xx as
  (select '333-22-234223' as a
   from dual)
select xx.a
from xx
where
  regexp_like(xx.a,'^[:digit:]{3}-[:digit:]{2}-[:digit:]{6}$');

您可以使用以下解决方案使用 [[:digit:]](双 [...]):

WITH xx AS (
    SELECT '333-22-234223' AS a FROM dual
)
SELECT xx.a
FROM xx
WHERE REGEXP_LIKE(xx.a, '^[[:digit:]]{3}-[[:digit:]]{2}\-[[:digit:]]{6}$');

... 或使用 [0-9] 而不是 [[:digit:]]:

WITH xx AS (
    SELECT '333-22-234223' AS a FROM dual
)
SELECT xx.a
FROM xx
WHERE REGEXP_LIKE(xx.a, '^[0-9]{3}-[0-9]{2}\-[0-9]{6}$');

demo: http://sqlfiddle.com/#!4/3149e4/120/1


为什么需要双括号?

These character classes are valid only inside the bracketed expression.
source: https://docs.oracle.com/cd/B12037_01/server.101/b10759/ap_posix001.htm

@Sebastion Brosch 的替代品

您可以将字符 class ([:digit:]) 替换为明确的数字范围,如下所示:

with xx as
(select '333-22-234223' as a
from dual)
select xx.a
from xx
where
regexp_like(xx.a,'^[0-9]{3}-[0-9]{2}-[0-9]{6}$');

为了完整起见,您也可以使用 \d 作为数字:

with xx as
  (select '333-22-234223' as a
   from dual)
select xx.a
from xx
where regexp_like(xx.a,'^\d{3}-\d{2}-\d{6}$');