-2
Pattern pattern = Pattern.compile("\\du\\d\\d{0,1}_x(32|64)");
Matcher matcher = pattern.matcher("windows_6u31_x32.jar");
System.out.println(matcher.group(0));

I'm trying to match the string "6u31_x32" and extract that but when I use this regex it fails for some reason. It throws "No match found".

I've tried going into debug mode and it can't seem to extract that specific part of the string. What am I doing wrong? Is my regex wrong? It seems to match on all the online regex builders/testers I've tried on.

orange
  • 5,297
  • 12
  • 50
  • 71

1 Answers1

2

You need to invoke Matcher#find (or Matcher#matches) before you can invoke Matcher#group.

Otherwise, the matching process never takes place and you get an IllegalStateException thrown.

From the documentation:

The explicit state of a matcher is initially undefined; attempting to query any part of it before a successful match will cause an IllegalStateException to be thrown. The explicit state of a matcher is recomputed by every match operation.

Change your code to:

Pattern pattern = Pattern.compile("\\du\\d\\d{0,1}_x(32|64)");
Matcher matcher = pattern.matcher("windows_6u31_x32.jar");
// executing matcher
if (matcher.find()) {
    System.out.println(matcher.group(0));
}

Output

6u31_x32
Mena
  • 47,782
  • 11
  • 87
  • 106