Java RegEx – get matches array or ArrayList example shows how to get all regex matches as an array or an ArrayList object in Java.
How to get regex matches as an ArrayList in Java?
In Java RegEx, there is no direct way to get all the matches as an array or an ArrayList. The process goes like first we create a pattern object to match. Then we create a matcher object to match the pattern against the input string. After that, we iterate over matches using the find
method of the Matcher class and get the matches using the group
method one by one.
However, we can write a utility method that converts all regex matches to an ArrayList as given below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 |
package com.javacodeexamples.regex; import java.util.ArrayList; import java.util.List; import java.util.regex.Matcher; import java.util.regex.Pattern; public class RegExMatchesToArrayList { public static void main(String[] args) { String str = "The numbers 101, 102, and 103 are all greater than 100"; List<String> listMatches = getAllMatchesAsList(str); System.out.println("Total number of matches: " + listMatches.size()); System.out.println("ArrayList of all Regex matches: " + listMatches); } private static List<String> getAllMatchesAsList(String str) { List<String> listMatches = new ArrayList<String>(); //create a pattern to extract numbers Pattern pattern = Pattern.compile("\\d+"); //create a matcher for input string Matcher matcher = pattern.matcher(str); //iterate over all matches while( matcher.find() ) { //add match to the list listMatches.add( matcher.group() ); } return listMatches; } } |
Output
1 2 |
Total number of matches: 4 ArrayList of all Regex matches: [101, 102, 103, 100] |
As you can see from the output, our pattern “\\d+” matched with all the numbers in a string that is then converted to the List by iterating over all the matches one by one.
If you are using Java version 9 or above, you can use the results
method of the Matcher class that returns a Stream of MatchResult objects. You can then convert that stream to an array or a List object as given below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 |
import java.util.ArrayList; import java.util.List; import java.util.regex.Matcher; import java.util.regex.Pattern; import java.util.stream.Collectors; import java.util.regex.MatchResult; import java.util.Arrays; public class RegExMatchesToArrayList { public static void main(String[] args) { String str = "The numbers 101, 102, and 103 are all greater than 100"; System.out.println("ArrayList of all Regex matches: " + getAllMatchesAsList(str)); System.out.println("Array of all Regex matches: " + Arrays.toString(getAllMatchesAsArray(str))); } //all regex matches to a List private static List<String> getAllMatchesAsList(String str) { Pattern pattern = Pattern.compile("\\d+"); Matcher matcher = pattern.matcher(str); return matcher.results().map(MatchResult::group).collect(Collectors.toList()); } //all regex matches to an array private static String[] getAllMatchesAsArray(String str) { Pattern pattern = Pattern.compile("\\d+"); Matcher matcher = pattern.matcher(str); return matcher.results().map(MatchResult::group).toArray(String[]::new); } } |
Output
1 2 |
ArrayList of all Regex matches: [101, 102, 103, 100] Array of all Regex matches: [101, 102, 103, 100] |
Please note that you need Java version 9 or above for this code to work.
If you want to learn more about regular expression in Java, please visit the Java regex tutorial.
Please let me know your views in the comments section below.