简体   繁体   中英

regexp_like to find 10 occurrences of 0's

with smpl as
(
    select '000-000-0000' num from dual union
    select '0000000000' from dual union
    select '00000000000' from dual
)
select * from smpl
where regexp_like(num, '0{10}');

Output:

0000000000
00000000000

How to get records with 10 occurences 0's with optional '-' Expected:

0000000000
000-000-0000

Use TRANSLATE to strip out the unwanted characters and then LENGTH

with smpl as
(
    select '000-000-0000' num from dual union
    select '0000000000' from dual union
    select '000000000' from dual union
    select '00000000000' from dual
)
select * from smpl
where LENGTH( TRANSLATE( num, '0-', '0' ) ) = 10

or compare to 0000000000 :

with smpl as
(
    select '000-000-0000' num from dual union
    select '0000000000' from dual union
    select '000000000' from dual union
    select '00000000000' from dual
)
select * from smpl
where TRANSLATE( num, '0-', '0' ) = '0000000000'

Outputs :

 |  NUM | |:----------- |  |  000-000-0000 |  |  0000000000 | 

db<>fiddle here

You could use:

regexp_like(replace(num, '-', ''), '^0{10}$')

This means: first remove all occurences of '-' , the results should be 10 consecutive 0 s.

This can also be expressed without regexes:

replace(num, '-', '') = '0000000000'

With regexp, this is an easy way:

where regexp_count(num, '0') = 10

However, if the only character different from '0' is a '-' , I would prefer the non-regexp solution

I want to point out that you can do this strictly with regular expressions. If you are looking for the pattern anywhere in the string:

where regexp_like(num, '(\-*0){10}')

If you are looking for only that pattern in the string:

where regexp_like(num, '^(\-*0){10}\-*$')

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM