Skip to content
Advertisement

Why does this regex fails to check accurately?

I have the following regex method which does the matches in 3 stages for a given string. But for some reason the Regex fails to check some of the things. As per whatever knowledge I have gained by working they seem to be correct. Can someone please correct me what am I doing wrong here?

I have the following code:

public class App {
    public static void main(String[] args) {
        String identifier = "urn:abc:de:xyz:234567.1890123";

        if (identifier.matches("^urn:abc:de:xyz:.*")) {
            System.out.println("Match ONE");

            if (identifier.matches("^urn:abc:de:xyz:[0-9]{6,12}.[0-9]{1,7}.*")) {
                System.out.println("Match TWO");

                if (identifier.matches("^urn:abc:de:xyz:[0-9]{6,12}.[a-zA-Z0-9.-_]{1,20}$")) {
                    System.out.println("Match Three");
                }
            }
        }

    }
}

Ideally, this code should generate the output

Match ONE
Match TWO
Match Three

Only when the identifier = "urn:abc:de:xyz:234567.1890123.abd12" but it provides the same output event if the identifier does not match the regex such as for the following inputs:

"urn:abc:de:xyz:234567.1890123"
"urn:abc:de:xyz:234567.1890ANC"
"urn:abc:de:xyz:234567.1890123"
"urn:abc:de:xyz:234567.1890ACB.123"

I am not understanding why is it allowing the Alphanumeric characters after the . and also it does not care about the characters after the second ..

I would like my Regex to check that the string has the following format:

  1. String starts with urn:abc:de:xyz:
  2. Then it has the numbers [0-9] which range from 6 to 12 (234567).
  3. Then it has the decimal point .
  4. Then it has the numbers [0-9] which range from 1 to 7 (1890123)
  5. Then it has the decimal point ..
  6. Finally it has the alphanumeric character and spcial character which range from 1 to 20 (ABC123.-_12).

This is an valid string for my regex: urn:abc:de:xyz:234567.1890123.ABC123.-_12

This is an invalid string for my regex as it misses the elements from point 6: urn:abc:de:xyz:234567.1890123

This is also an invalid string for my regex as it misses the elements from point 4 (it has ABC instead of decimal numbers). urn:abc:de:xyz:234567.1890ABC.ABC123.-_12

Advertisement

Answer

This part of the regex:

  • [0-9]{6,12}.[0-9]{1,7} matches 6 to 12 digits followed by any character followed by 1 to 7 digits

To match a dot, it needs to be escaped. Try this:

^urn:abc:de:xyz:[0-9]{6,12}.[0-9]{1,7}.[a-zA-Z0-9-_]{1,20}$
User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement