Partner – Moderne – NPI EA (cat=Spring Boot)
announcement - icon

Refactor Java code safely — and automatically — with OpenRewrite.

Refactoring big codebases by hand is slow, risky, and easy to put off. That’s where OpenRewrite comes in. The open-source framework for large-scale, automated code transformations helps teams modernize safely and consistently.

Each month, the creators and maintainers of OpenRewrite at Moderne run live, hands-on training sessions — one for newcomers and one for experienced users. You’ll see how recipes work, how to apply them across projects, and how to modernize code with confidence.

Join the next session, bring your questions, and learn how to automate the kind of work that usually eats your sprint time.

Course – Black Friday 2026 – NPI EA – main – before_post_content
announcement - icon

Yes, we're now running our Black Friday Sale. All Courses are 33% off until 4th December, 2026 at 17:00 Romania time:

>> EXPLORE ACCESS NOW

Course – Black Friday 2026 – NPI – main – before_post_content
announcement - icon

Yes, we're now running our Black Friday Sale. All Courses are 33% off until 4th December, 2026 at 17:00 Romania time:

>> EXPLORE ACCESS NOW

1. Overview

In this tutorial, we’ll learn how to create an array from a regular expression (regex) output.

2. Introduction

For our example, let’s parse a long string. We’ll find patterns with 10-digit phone numbers. We’ll then have the output generated as an array.

Oracle has provided java.util.regex package for its regex implementation. We’ll use the classes available in this package for our demo. Once we find our matches, we’ll take that output and create an array.

Arrays are fixed-size variables. We must declare their size before using them. There is also a chance of memory wastage if arrays are not correctly implemented. For this reason, we start with a List and later convert the List dynamically to an array.

3. Implementation

Let’s go through our codes to implement this solution step-by-step. To start, let’s create an ArrayList to store the matches:

List<String> matchesList = new ArrayList<String>();

We’ll store a long string with phone numbers embedded in it as follows:

String stringToSearch =
  "7801111111blahblah  780222222 mumbojumbo7803333333 thisnthat 7804444444";

We use compile() method, a static factory method in the Pattern class. It returns an equivalent Pattern object of regex:

Pattern p1 = Pattern.compile("780{1}\\d{7}");

Once we have a Pattern object, we create a Matcher object using the matcher() method:

Matcher m1 = p1.matcher(stringToSearch); 

Here we can use find() method from the Matcher class, which returns a boolean if a match is found:

while (m1.find()) {
    matchesList.add(m1.group());
}

The group() method we just used is in the Matcher class. It produces a String that represents the matched pattern.

To convert matchesList to an array, we find the number of items that we matched. Then we use it when we create a new array to store the results:

int sizeOfNewArray = matchesList.size(); 
String newArrayOfMatches[] = new String[sizeOfNewArray]; 
matchesList.toArray(newArrayOfMatches);

Let’s now see how our code works with some examples. If we pass a String with four matching patterns, our code produces a new String array with these four matches:

RegexMatches rm = new RegexMatches();
String actual[] = rm.regexMatch("7801111211fsdafasdfa  7802222222  sadfsadfsda7803333333 sadfdasfasd 7804444444");

assertArrayEquals(new String[] {"7801111211", "7802222222", "7803333333", "7804444444"}, actual, "success");

If we pass a String with no matches, we get an empty String array:

String actual[] = rm.regexMatch("78011111fsdafasdfa  780222222  sadfsadfsda78033333 sadfdasfasd 7804444");

assertArrayEquals(new String[] {}, actual, "success");

4. Conclusion

In this tutorial, we learnt how to look for patterns in a string of text in Java. We also found a way to list the outputs in an array.

The code backing this article is available on GitHub. Once you're logged in as a Baeldung Pro Member, start learning and coding on the project.
Partner – Moderne – NPI EA (tag=Refactoring)
announcement - icon

Modern Java teams move fast — but codebases don’t always keep up. Frameworks change, dependencies drift, and tech debt builds until it starts to drag on delivery. OpenRewrite was built to fix that: an open-source refactoring engine that automates repetitive code changes while keeping developer intent intact.

The monthly training series, led by the creators and maintainers of OpenRewrite at Moderne, walks through real-world migrations and modernization patterns. Whether you’re new to recipes or ready to write your own, you’ll learn practical ways to refactor safely and at scale.

If you’ve ever wished refactoring felt as natural — and as fast — as writing code, this is a good place to start.

Course – Black Friday 2026 – NPI EA – main – after_post_content

announcement - icon

Yes, we're now running our Black Friday Sale. All Courses are 33% off until 4th December, 2026 at 17:00 Romania time:

>> EXPLORE ACCESS NOW

Course – Black Friday 2026 – NPI – main – after_post_content

announcement - icon

Yes, we're now running our Black Friday Sale. All Courses are 33% off until 4th December, 2026 at 17:00 Romania time:

>> EXPLORE ACCESS NOW