I am trying to implement custom regular expressions in my Java program to verify if my input string matches the regex. But for some reason, the regex fails at a particular point even though the input string matches it.
String input = "abc:def:id:identifier:123456.890123.1";
System.out.println(input.matches("(abc:def:id:identifier:).[0-9]{6,12}.[0-9]{1,6}.[0-9]{1,20}"));
The regex returns false
at the point 123456
even though the input matches the length of 6.
If I give 1234567
then it would return true
. I am wondering why it fails for 123456
even though it matches the length. Also, it does not fail for the next one where I have just 1
and length of {1,20}
.
Also, is there a better way to verify this string in regex or this is a good and efficient way?