Kate Kate - 3 months ago 7
Java Question

regex to filter contact numbers

I've a list of phone numbers, but want to remove phone numbers on basis of some condition. I'm trying to set up in ETL

Lets say below is the list of contact numbers

897-54-6212
985566655
568-00-9977
000-56-8795
875-95-9999
&^*-(&-#$@)
111-85-4568
222-56-6542
333-56-4589
444-48-8978
555-48-8978
666-58-5648
777-58-5647
888-86-8978
456-33-5685
485-44-4568
465-55-8523
485-66-7824
468-77-5948
854-88-2358
785-78-1111
588-64-2222
078-05-1120
455-56-(0000)


And I dont want numbers like if it contains:
1) The first three digits should not contains 000 or 666 or 900 or 999 (for example 666-77-5948 should be rejected)

2) Number should also ne rejected if it contains 00 in between (for example 945-00-8796)

3) If number contains any kind of special character like @!#$%^()

Is that possible kind of some regex so that I could filter kind of these records. I've tried with substrings function but that's didnt work as expected.

These number should be rejected ...

985566655
568-00-9977
000-56-8795
&^*-(&-#$@)
666-58-5648
455-56-(0000)


Can anyone help ?

Answer

The special characters can be ruled out by using a regex that specifies only numbers and dashes:

^\d{3}-\d{2}-\d{4}$

If you add on negative lookbehinds to blacklist certain numbers, it would look like this:

^\d{3}(?<!000|666|999|900)-\d{2}(?<!00)-\d{4}$

Regex101 Demo

Since you will be matching this against a list, make sure to precompile the regex using a Pattern object. Don't use String#matches() in a loop.