Match found only in debug mode

Viewed 46

I have a method for swapping of matched part of string with current date:

public static String swapDateOnConstant(String value) {
    LOGGER.info("Swap date in string: " + value);
    String patternString = "<(.*)>";
    Pattern pattern = Pattern.compile(patternString);
    Matcher matcher = pattern.matcher(value);
    String dateFormat = matcher.group(0);
    dateFormat = dateFormat.substring(1, dateFormat.length() - 1)
            .replaceAll("D", "d")
            .replaceAll("m", "M")
            .replaceAll("Y", "y");
    String currentDate = DateTimeTools.getCurrentDateInFormat(dateFormat);
    value = value.replaceAll(patternString, currentDate);
    return value;}

Input string value is: \<MM-DD-YY> CITI

When I go through this method in debug method everything is ok but when I's run in normal mode I receive and exception java.lang.IllegalStateException: No match found

from line String dateFormat = matcher.group(0);

I'm confused. Any idea what is going on here ?

1 Answers

Matcher requires you to search for a match before retrieving groups. So you should be calling something like matches() to match entire string or find() to get a partial match.

Matcher matcher = pattern.matcher(value);
if (matcher.find()) {
  matcher.group(0);
}

Not sure why your debugger works without a method for finding matches. Debug in Eclipse returned the expected IllegalStateException error.

Related