4

I have 3 categories of telephone number that is Golden, Special and Normal. What I'm trying to do is when the user key in the telephone number, It will automatically determine the telephone number belongs to which category. Let me give one example of Golden category number : AA001234 (AA represents 2 digits with the same number like 11,22,33 etc.). Here what I got

public static void main(String[] args) {

    Scanner userinput = new Scanner(System.in);

    System.out.println("Enter Telephone Number");
    String nophone = userinput.next();

    String Golden = "(\\d{2})002345"; // <-- how to write the code if the user
    //enter the same digit for the first 2 number, it will belong to Golden category?
    String Special1 = "12345678|23456789|98765432|87654321|76543210";

    if (nophone.matches(Golden)) {
        System.out.println("Golden");
    }

    else if (nophone.matches(Special1)) {
        System.out.println("Special 1");
    }
    else {
        System.out.println("Normal");
    }
}
Kara
  • 6,115
  • 16
  • 50
  • 57
  • 1
    Check this post: http://stackoverflow.com/questions/123559/a-comprehensive-regex-for-phone-number-validation – rharrison33 Nov 07 '12 at 03:47
  • thanks but that post is more to the phone number format. I was looking for the pattern of telephone number (as I mentioned above) so that I can categorize it to the particular category – Mista PlatoniC Nov 07 '12 at 05:55

3 Answers3

2

I am not sure Java support full regex implementation, but if it does, you can use:

(\d)(\1)002345

\1 means back reference to the first match (parenthesis-ed), so (\d)(\1) will match two same numbers consecutively.

If Java does not support this, I suggest you to hard code it since you only have 3 categories.

SwiftMango
  • 15,092
  • 13
  • 71
  • 136
  • how if the pattern is like this: AB001234 where AB represents number like 12,23,34 etc? – Mista PlatoniC Nov 07 '12 at 05:46
  • @MistaPlatoniC regex does not do math. It does not know 1,2 are adjacent. You will need to construct a string in Java and hard code it. – SwiftMango Nov 07 '12 at 06:26
  • Can you check this one: String AABBCC = "(\\d)\\2(\\d)\\3(\\d)\\4"; String ABABAB = "(\\d\\d)\\5\\5$"; String ABABABAB = "(\\d\\d)\\6\\6\\6$"; What number pattern falls into these category? – Mista PlatoniC Nov 07 '12 at 06:41
  • @MistaPlatoniC the pattern is right but make sure you number the back reference from 1 in each pattern. – SwiftMango Nov 07 '12 at 14:03
1

You can use back-reference like (\\d)\\1. (e.g. (\\d)\\1\\d*).

Where

  1. the first \\d means a digit
  2. \\1 means the same digit and
  3. \\d* means 0 or more digit(s).
Bhesh Gurung
  • 50,430
  • 22
  • 93
  • 142
  • just curious whats is the meaning of \\2, \\3 etc? – Mista PlatoniC Nov 07 '12 at 04:24
  • `(\\d)` is a group of one digit, once matched it is saved in the memory and can be back referenced, by `\\1`. That means if it was 1 then the following digit must be 1 also. You can read about it hear - http://docs.oracle.com/javase/tutorial/essential/regex/groups.html – Bhesh Gurung Nov 07 '12 at 04:33
  • how if the number pattern is like this: AB001234 where AB represents number like 12,23,34 etc? – Mista PlatoniC Nov 07 '12 at 06:00
  • @MistaPlatoniC: You would have to handle that with code instead of regex. – Bhesh Gurung Nov 07 '12 at 06:03
  • @MistaPlatoniC: Just give it a try, if it doesn't work then come back here and ask a new question, I am sure you will get the help. – Bhesh Gurung Nov 07 '12 at 06:16
1

If the length of the number is not a matter you can use this. Since you are using java you need two slashes.
String Golden = "(\\d)\\1\\d*";

If the length of the number should exactly be eight

String Golden = "(\\d)\\1\\d{6}";

If you want to match five repeated numbers,
String Golden = "(\\d)\\1{4}\\d*";

prageeth
  • 7,159
  • 7
  • 44
  • 72