The mistake of calling matcher.goup() without a prior call to matcher.find(), matcher.matches(), or matcher.lookingAt()

This annoying mistake is found in many programs. As the heading says, these are cases where programmers call any of the group() methods without a prior call to the find, matches, or lookingAt methods. A matcher is created using the pattern.matcher(String) method call, but we need to invoke one of these three methods to perform a match operation.

If we call matcher.group() without calling one of these three methods, then the code will throw a java.lang.IllegalStateException exception, as the following code is doing:

 package example.regex; import java.util.regex.*; public class MissingMethodCall { public static void main(String[] ...

Get Java 9 Regular Expressions now with the O’Reilly learning platform.

O’Reilly members experience live online training, plus books, videos, and digital content from nearly 200 publishers.