The matcher group() is used for matching the substring in Java. The matcher class in Java is used for matching the specific input sequence with the pattern provided. Therefore, the matcher class has been beneficial in Java to search for the given pattern. If the pattern is matched with the sequence then the output is returned accordingly.
How to Implement/Create the Java Matcher group() Method?
The group() method of the matcher class in Java is used to match the input from the pattern and return the matching values. Let us suppose we want to find out how many times the element e has appeared in Hello to everyone, we will implement the group() method of the Java matcher class to get the output as element e printed four times.
Syntax of group() Method
public String group()
public String group(int group)
Let us implement the above syntax of the group() method in the code below:
Example 1: Using group() Method
The following example considers the matcher group() implementation in Java to extract the text that matched a regular expression from the string.
import java.util.regex.*;
//Create a class for matching strings
public class groupmethod {
public static void main(String[] args)
{
//Mention the strings
String matchingregex = "(H*e)";
Pattern matchingpattern= Pattern.compile(matchingregex);
String matchedstring = "Hi to everyone";
//Statement for matching strings
Matcher match= matchingpattern.matcher(matchedstring);
// The output for the strings matched
MatchResult output= match.toMatchResult();
//Print the results on the screen
System.out.println("The current match is: ");
//When the match is found using the group() method
while (match.find()) {
System.out.println(match.group());
}
}
}
In the code above:
- java.util.regex is used to get the pattern that matches with the string provided.
- The compile method() of the pattern class is used to design a pattern for expression.
- The matcher class matches the given elements from the given pattern.
- The group() method then returns the matching elements in the pattern.
Output

In the above output, the matching element e in the given pattern has been printed.
Example 2: Using Integer Value in the group() Method
In the following code, we use integer value instead of a string to match it with a regular expression from a string using the group() method in Java.
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class integergroup {
public static void main(String[] args) {
//Enter input
String intinput ="Your age is 32";
// Enter the matching integer with the strings
Pattern matchpattern = Pattern.compile("Your age is ([0-9]+)");
Matcher matcher = matchpattern.matcher(intinput);
//If the strings are matching
if (matcher.find()) {
String age = matcher.group(1);
//Print the output
System.out.println("The age is : " + age);
}
}
}
The group() method of the matcher class has printed the results successfully.
Output

In the above output, the age has been printed accordingly. However, if you fail to pass any argument with the Pattern.compile() function, it will lead to a compilation error.
Consider the following example to verify this:
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class integergroup {
public static void main(String[] args) {
//Enter input
String intinput ="Your age is 32";
// Enter the matching integer with the strings
Pattern matchpattern = Pattern.compile("");
Matcher matcher = matchpattern.matcher(intinput);
//If the strings are matching
if (matcher.find()) {
String age = matcher.group(1);
//Print the output
System.out.println("The age is : " + age);
}
}
}
Output

In these above output, the compiler throws an illegal exception of IndexOutOfBoundsException since no argument has been passed during pattern compilation.
Conclusion
The Java matcher group() works effectively to match the elements present in the given pattern. The strings and the integers values can be easily matched from the matcher class of Java using the group() method. This article has effectively demonstrated the group() method implementation on both strings and integers.