Reputation: 63
Oracle Regular Expression To match US Phone Number in the following formats only.
(NNN) NNN-NNNN
or NNN-NNN-NNNN
or NNNNNNNNNN
I have tried and came up to this :
with test as
(
select '(444) 123-6780' as testcol from dual
union
select '444123-6780' from dual
union
select '6741236780' from dual
union
select '(445) 123-6781' from dual
union
select '447-127-6787' from dual
union
select '447-127-3333333333' from dual
)
SELECT *
FROM test
WHERE REGEXP_LIKE(testcol, '^\(?\d{3}\)?([[:blank:]|-])?\d{3}-?\d{4}$');
Results :
(444) 123-6780 -- valid
(445) 123-6781 -- valid
444123-6780 -- not valid
447-127-6787 -- valid
6741236780 -- valid
As you can see, I received 444123-6780
(NNNNNN-NNNN
) format,
which I don't require.
Is there any way to stop the above mentioned format not to match?
Upvotes: 3
Views: 14690
Reputation: 63
REGEXP_LIKE(testcol,'^(\(\d{3}\))([[:blank:]])\d{3}-\d{4}$|^\d{3}(-)\d{3}(-)\d{4}$|^\d{10}$')
Upvotes: 1
Reputation: 9126
try this reg ex.. it will help you
REGEXP_LIKE(testcol,'^\([0-9]{3}\)[:space:]?[0-9]{3}(-|[:space:])?[0-9]{4}$|^[0-9]{3}-?[0-9]{3}-?[0-9]{4}$')
Source From : http://psoug.org/snippet/Regular-Expressions--RegExp-examples-3_859.htm
Upvotes: 0