-3

I need to generate regex to validate state as Tamilnadu based on Pincode validation

Regex which I tried fails at some point

String regex = "^[60-64]{2}{0-9}{4}$";

Ref the Tamil Nadu Pincode info link. It starts with 60-64 as the first two digits, the next 4 digits as 0-9 numbers. It must have six digits.

the code

public boolean isHomeState(String state, String zipcode) {
    if (isValidZipCode(zipcode)) { 
    // ...
    }
    return true;  
}

private boolean isValidZipCode(String zipcode) {
    String regex = "^[60-64]{2}{0-9}{4}$";

    Pattern p = Pattern.compile(regex);
    // If the pin code is empty
    // return false
    if (zipcode == null) {
        return false;
    }

    Matcher m = p.matcher(zipcode);
    return m.matches();
}
Arvind Kumar Avinash
  • 71,965
  • 6
  • 74
  • 110
selvi
  • 1,271
  • 2
  • 21
  • 41
  • 3
    " fails at some point " - Can you give more details? – Abhishek Ramachandran Nov 05 '20 at 13:51
  • A [mcve] with inputs and expected output would help. Without the need to reference an external link that just has a long list, with no explanation. – Robert Nov 05 '20 at 15:17
  • 1
    Your regex says `[60-64]`. A regular expression is character based, it doesn't know numbers. That means this is treated as a group of 6, 0 to 6, and 4, i.e., 6, 0, 1, 2, 3, 4, 5, 6, 4. You want `6[0-4]` as @Abra wrote. – Robert Nov 05 '20 at 15:20

2 Answers2

2

Try the following.

Pattern pattern = Pattern.compile("6[0-4]\\d{4}");

In other words, the digit 6 followed by a digit that is either 0 or 1 or 2 or 3 or 4 and ending with exactly four digits.

Abra
  • 19,142
  • 7
  • 29
  • 41
0

There are many regex patterns to do it e.g.

  1. 6[0-4][0-9]{4} which means 6 followed by a digit in the range, 0 to 4 which in turn followed by exactly 4 digits.
  2. 6[0-4]\d{4} which means 6 followed by a digit in the range, 0 to 4 which in turn followed by exactly 4 digits
  3. 6[0-4][0-9][0-9][0-9][0-9] which means 6 followed by a digit in the range, 0 to 4 which in turn followed by exactly 4 digits

However, let's analyze your regex, [60-64]{2}[0-9]{4} which will help you understand the problem better.

The regex, [60-64] means only one of the following:

  1. 6
  2. A digit in the range 0 to 6
  3. 4

And [60-64]{2} means the above repeated exactly two times i.e. it will match with a combination of 2 digits in the range, 0 to 6 e.g. 00, 60, 34, 01, 10, 11 etc.

As a result, the regex, [60-64]{2}[0-9]{4} will match with first 2 digits consisting of digits in the range, 0 to 6 and next 4 digits consisting of digits in the range, 0 to 9 e.g.

012345
123856
234569
101010
202020
303030
404040
505050
606060
111111
222222
333333

which is not something you expect.

Arvind Kumar Avinash
  • 71,965
  • 6
  • 74
  • 110