Reputation: 6566
I'm trying the following regexp_like for a bigger query, it does not work, what am i doing wrong?
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}$');
Upvotes: 1
Views: 87
Reputation: 10360
For the sake of completeness, you can use \d
for a digit as well:
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}$');
Upvotes: 1
Reputation: 43574
You can use the following solution using [[:digit:]]
(double [...]
):
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}$');
... or using [0-9]
instead of [[: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}$');
Why does it require double brackets?
These character classes are valid only inside the bracketed expression.
source: https://docs.oracle.com/cd/B12037_01/server.101/b10759/ap_posix001.htm
Upvotes: 3
Reputation: 2441
An alternative to @Sebastion Brosch
You can replace the character class ([:digit:]
) with an explicit range of digits, like this:
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}$');
Upvotes: 2