O'Reilly logo

Java 9 Regular Expressions by Anubhava Srivastava

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

The matches() method

The matches() method attempts to match the entire region against the pattern and returns true only if the entire region matches against the pattern.

Let's look at the following code to understand the use of this method better:

package example.regex; import java.util.regex.*; class MatcherMatchesExample {   public static void main (String[] args)   {     final Pattern pattern1 = Pattern.compile("mastering");     final Pattern pattern2 = Pattern.compile("mastering.*");     final Pattern pattern3 = Pattern.compile("regular.*");     String input = "mastering regular expressions";     Matcher matcher = pattern1.matcher(input);     System.out.printf("[%s] => [%s]: %s%n", input, matcher.pattern(),     matcher.matches());  // update the matcher ppattern ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required