-7

I want to match phone numbers like this,

It should have 3 digits except 000,666 and any numbers between 900-999 followed by "-", then 2 digits followed by "-", then 4 digits.

For example:

123-75-3456  is a match
000- 23-3452 is not a match (no 000)
915-23-4534 is not a match (greater than 900)

This is where I currently stand:

[0-9&&^[000,666,[900-999]]{3}-[0-9]{2}-[0-9]{4}
PhilMasteG
  • 3,095
  • 1
  • 20
  • 27
Bhargav
  • 697
  • 3
  • 11
  • 29

1 Answers1

2

I think this one should do the trick:

^(?!000|666|9\d{2})\d{3}-\d{2}-\d{4}$

Edit: I find the negative look-ahead syntax in this thread.

Edit 2: Here is a little code snippet for those who want to test it:

import java.util.Scanner;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class Main {

    public static void main(String[] args) {
        Pattern pattern = Pattern.compile("^(?!000|666|9\\d{2})\\d{3}-\\d{2}-\\d{4}$");
        Scanner sc = new Scanner(System.in);
        while (true) {
            System.out.println("Next number :");
            Matcher matcher = pattern.matcher(sc.nextLine());
            if (matcher.find()) {
                System.out.println("Matches");
            } else {
                System.out.println("Doesn't match");
            }
        }
    }
}
Abrikot
  • 965
  • 1
  • 9
  • 21