Expand Authors Top

If you have a few years of experience in the Java ecosystem and you’d like to share that with the community, have a look at our Contribution Guidelines.

Expanded Audience – Frontegg – Security (partner)
announcement - icon User management is very complex, when implemented properly. No surprise here.

Not having to roll all of that out manually, but instead integrating a mature, fully-fledged solution - yeah, that makes a lot of sense.
That's basically what Frontegg is - User Management for your application. It's focused on making your app scalable, secure and enjoyable for your users.
From signup to authentication, it supports simple scenarios all the way to complex and custom application logic.

Have a look:

>> Elegant User Management, Tailor-made for B2B SaaS

November Discount Launch 2022 – Top
We’re finally running a Black Friday launch. All Courses are 30% off until the end of this week:


November Discount Launch 2022 – TEMP TOP (NPI)
We’re finally running a Black Friday launch. All Courses are 30% off until the end of this week:


1. Overview

Regular expressions can be used for a variety of text processing tasks, such as word-counting algorithms or validation of text inputs.

In this tutorial, we'll take a look at how to use regular expressions to count the number of matches in some text.

2. Use Case

Let's develop an algorithm capable of counting how many times a valid email appears in a string.

To detect an email address, we'll use a simple regular expression pattern:


Note that this is a trivial pattern for demonstration purposes only, as the actual regex for matching valid email addresses is quite complex.

We'll need this regular expression inside a Pattern object so we can use it:


We'll look at two main approaches, one of which depends on using Java 9 or later.

For our example text, we will try to find the three emails in the string:

"You can contact me through [email protected], [email protected], and [email protected]"

3. Counting Matches for Java 8 and Older

Firstly, let's see how to count the matches using Java 8 or older.

A simple way of counting the matches is to iterate over the find method of the Matcher class. This method attempts to find the next subsequence of the input sequence that matches the pattern:


int count = 0;
while (countEmailMatcher.find()) {

Using this approach, we'll find three matches, as expected:

assertEquals(3, count);

Note that the find method does not reset the Matcher after every match found — it resumes starting at the character after the end of the previous sequence matched, so it wouldn't work to find overlapping email addresses.

For instance, let's consider this example:

String OVERLAPPING_EMAIL_ADDRESSES = "Try to contact us at [email protected]@baeldung.com, [email protected]";

Matcher countOverlappingEmailsMatcher = EMAIL_ADDRESS_PATTERN.matcher(OVERLAPPING_EMAIL_ADDRESSES);

int count = 0;
while (countOverlappingEmailsMatcher.find()) {

assertEquals(2, count);

When the regex tries to find matches in the given String, first it'll find “[email protected]” as a match. Since there's no domain part preceding the @, the marker won't get reset and the second “@baeldung.com” will get ignored. Moving on, it will also consider “[email protected]” as the second match:

match regex

As shown above, we only have two matches in the overlapping email example.

4. Counting Matches for Java 9 and Later

However, if we have a newer version of Java available, we can use the results​ method of the Matcher class. This method, added in Java 9, returns a sequential stream of match results, allowing us to count the matches more easily:

long count = countEmailMatcher.results()

assertEquals(3, count);

Like we saw with find, the Matcher is not reset while processing the stream from the results method. Similarly, the results method wouldn't work to find matches that overlap, either.

5. Conclusion

In this short article, we've learned how to count the matches of a regular expression.

Firstly, we learned how to use the find method with a while loop. Then we saw how the new Java 9 streaming method allows us to do this with less code.

As always, the code samples are available over on GitHub.

November Discount Launch 2022 – Bottom
We’re finally running a Black Friday launch. All Courses are 30% off until the end of this week:


Generic footer banner
Inline Feedbacks
View all comments
Comments are closed on this article!