javamatcher

Matcher group() function returns null


I have this regular expression "([0-9TA])?(\\d{4})?([A-Z]{3})?" and the string "52021CCC".

String testNumber = "52021CCC";
String[] parts = null;

String regex = "([0-9TA])?(\\d{4})?([A-Z]{3})?";
Pattern pattern = Pattern.compile(regex);
Matcher matcher = pattern.matcher(testNumber);
if (matcher.matches() || matcher.find()) {
    parts = new String[3];
    parts[0] = matcher.group(1);
    parts[1] = matcher.group(2);
    parts[2] = matcher.group(3);
    System.out.println(parts[0]);
    System.out.println(parts[1]);
    System.out.println(parts[2]);
}

I expected to print : 5, 2021, CCC

but it prints null, null, null.

When I change the input string to 5T2021CCC, it prints: T, 2021, CCC

My question is, since the first group is a single digit or the charaters T or A shouldnt the 52021CCC be matched and printed correctly?

I cannot understand where is the problem here.


Solution

  • The problem is in the expression in the if:

    if (matcher.matches() || matcher.find()) {
    

    Since matcher.matches() returns true and this is a logical or, this expression short-circuits and the matcher.find() is never executed. This, all calls to group() will return null.

    A simple fix is to replace the or with an and:

    if (matcher.matches() && matcher.find()) {
    

    In this case it might actually be enough to only call find and remove the matches check.