Home » Java » Core Java » Matching patterns with Java

About Sebastian Daschner

Sebastian Daschner
Sebastian Daschner is a self-employed Java consultant and trainer. He is the author of the book 'Architecting Modern Java EE Applications'. Sebastian is a Java Champion, Oracle Developer Champion and JavaOne Rockstar.

Matching patterns with Java

If you’re using Java, there’s a high chance you’ve seen its pattern matching before. The String#matches(String) method internally uses the Pattern type, which comprises more complex functionality:

A Pattern is created by compiling a regular expression. The pattern matches any input string and can optionally find capturing groups, which isolate certain parts of your string data.

The API is used as follows:

Pattern pattern = Pattern.compile("([\\^\\S]+) is powerful");
Matcher matcher = pattern.matcher("Java is powerful");
System.out.println(matcher.find()); // true
System.out.println(matcher.group()); // Java is powerful
System.out.println(matcher.group(1)); // Java

The find() method finds the next occurrence of the pattern, which matches the whole input string in this example. The group() method returns either the whole capturing group, that is, matching the whole pattern, or, when qualified with an index, returns the individual capturing groups. The capturing groups indexes start at 1, not at 0.

There’s also a matches() method which works slightly differently:

Pattern pattern = Pattern.compile("([\\^\\S]+) is powerful");
Matcher matcher = pattern.matcher("Our Java is powerful");
System.out.println(matcher.matches()); // false
System.out.println(matcher.find()); // true

matches() attempts to match the whole input string to the pattern, from start to end, while find() only tries to find the patterns somewhere in the input string.

Also, as reminder: Please use the shortcut methods String#matches(String) or Pattern#matches(String, CharSequence) only for single matching invocations that are not repeated over and over again. Patterns are rather heavy to compile and we should leverage the immutability of the Pattern type and reuse it for multiple matches.

The content of this post was reposted from my newsletter issue 034.

Published on Java Code Geeks with permission by Sebastian Daschner, partner at our JCG program. See the original article here: Matching patterns with Java

Opinions expressed by Java Code Geeks contributors are their own.

(0 rating, 0 votes)
You need to be a registered member to rate this.
2 Comments Views Tweet it!
Do you want to know how to develop your skillset to become a Java Rockstar?
Subscribe to our newsletter to start Rocking right now!
To get you started we give you our best selling eBooks for FREE!
1. JPA Mini Book
2. JVM Troubleshooting Guide
3. JUnit Tutorial for Unit Testing
4. Java Annotations Tutorial
5. Java Interview Questions
6. Spring Interview Questions
7. Android UI Design
and many more ....
I agree to the Terms and Privacy Policy
Notify of

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Newest Most Voted
Inline Feedbacks
View all comments
2 years ago

Thanks for sharing useful information article and keep us updated on the topic

1 year ago

I’m Elena gillbert.Java provides the java.util.regex package for pattern matching with regular expressions. Java regular expressions are very similar to the Perl programming language and very easy to learn. Java regular expressions are very similar to the Perl programming language and very easy to learn.