Course – LS – All

Get started with Spring and Spring Boot, through the Learn Spring course:

>> CHECK OUT THE COURSE

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 source code is available over on GitHub.

Course – LS – All

Get started with Spring and Spring Boot, through the Learn Spring course:

>> CHECK OUT THE COURSE
res – REST with Spring (eBook) (everywhere)
Comments are closed on this article!