MatchResult Interface in Java
Last Updated :
18 Feb, 2022
Interface in java is used to achieve abstraction. It is also considered as the blueprint for the class. An interface can contain abstract methods only but it might or might not contain variables. In other words, an interface can contain abstract methods and variables only. Java supports multiple inheritance through interfaces only. Thus, an interface has a wider scope in Java.
This article focuses upon the MatchResult Interface.
MatchResult Interface:
The MatchResult interface in java signifies the result or conclusion of a match operation. It contains the definition of certain query methods that can be used to determine the result of the match with a regular expression. Note that we cannot alter the group/group boundaries and match boundaries using the MatchResult interface but one can see them easily through the interface. This interface was introduced in Java version 1.5.
This interface can be declared by using the below syntax,
Syntax:
public interface MatchResult
Methods of MatchResult Interface
The query methods of the MatchResult interface are discussed in detail below.
1. start() method: This method is used to return the start index of the match.
Syntax:
int start()
Return Value:
- integer: Represents the index of the first character that has a match
Example:
Java
import java.io.*;
import java.util.regex.MatchResult;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
class GFG {
private static final String regularExpression
= "(.*)(\\d+)(.*)" ;
private static final String input
= "Hello World!, 41346, these are the numbers." ;
public static void main(String[] args)
{
Pattern myPattern
= Pattern.compile(regularExpression);
Matcher myMatcher = myPattern.matcher(input);
if (myMatcher.find()) {
MatchResult myResult
= myMatcher.toMatchResult();
System.out.println(
"Starting index of the match: "
+ myResult.start());
}
}
}
|
Output
Start index of the match: 0
2. start(int group): This method returns the starting index of the sequence that has been captured by the given group during the match.
Syntax:
int start(int index)
Parameters:
- index: It represents the index of the capturing group in the matcher’s pattern.
Return Value:
- non-negative integer: It represents the index of the first character held by the group
- -1: Only when the match is successful but the group don-not match anything
Example:
Java
import java.util.regex.MatchResult;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class GFG {
private static final String regularExpression
= "(.*)(\\d+)(.*)" ;
private static final String input
= "Hello World!, 41346, these are the numbers." ;
public static void main(String[] args)
{
Pattern myPattern
= Pattern.compile(regularExpression);
Matcher myMatcher = myPattern.matcher(input);
if (myMatcher.find()) {
MatchResult myResult
= myMatcher.toMatchResult();
System.out.println( "Second Capturing Group: "
+ myResult.start( 1 ));
}
}
}
|
Output
Second Capturing Group: 0
3. end(): This method returns the offset when the last character is matched.
Syntax:
int end()
Return Type: It returns the offset after the last character matched.
Example:
Java
import java.io.*;
import java.util.regex.MatchResult;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
class GFG {
private static final String regularExpression
= "(.*)(\\d+)(.*)" ;
private static final String input
= "Hello World!, 41346, these are the numbers." ;
public static void main(String[] args)
{
Pattern myPattern
= Pattern.compile(regularExpression);
Matcher myMatcher = myPattern.matcher(input);
if (myMatcher.find()) {
MatchResult myResult
= myMatcher.toMatchResult();
System.out.println( "First Capturing Group: "
+ myResult.end());
}
}
}
|
Output
First Capturing Group: 43
4. end(int index): This method returns the offset when the last character is matched.
Syntax:
int end(int index)
Parameter:
- index: It represents the index of the capturing group in the matcher’s pattern.
Return Type: It returns the offset after the last character matched.
Example:
Java
import java.io.*;
import java.util.regex.MatchResult;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
class GFG {
private static final String regularExpression
= "(.*)(\\d+)(.*)" ;
private static final String input
= "Hello World!, 41346, these are the numbers." ;
public static void main(String[] args)
{
Pattern myPattern
= Pattern.compile(regularExpression);
Matcher myMatcher = myPattern.matcher(input);
if (myMatcher.find()) {
MatchResult myResult
= myMatcher.toMatchResult();
System.out.println( "Second Capturing Group: "
+ myResult.end( 1 ));
}
}
}
|
Output
Second Capturing Group: 18
5. group(): This method returns the input sequence that was matched with the previous match.
Syntax:
String group()
Return Type:
- A string: The sequence that was matched by the previous match.
Example:
Java
import java.io.*;
import java.util.regex.MatchResult;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
class GFG {
private static final String regularExpression
= "(.*)(\\d+)(.*)" ;
private static final String input
= "Hello World!, 41346, these are the numbers." ;
public static void main(String[] args)
{
Pattern myPattern
= Pattern.compile(regularExpression);
Matcher myMatcher = myPattern.matcher(input);
if (myMatcher.find()) {
MatchResult myResult
= myMatcher.toMatchResult();
System.out.println( "First Capturing Group"
+ myResult.group());
}
}
}
|
Output
First Capturing GroupHello World!, 41346, these are the numbers.
6. group(int index): This method is used to return the input sequence that was held by the specified group at the time of the previous match operation.
Syntax:
int group(int index)
Parameters:
- index: The index of the group that is being captured in this matcher’s pattern.
Return Type:
- A string: It represents a sequence that has been captured by the group at the time of the previous match
- null: If the group cannot match any part of the input.
Example:
Java
import java.io.*;
import java.util.regex.MatchResult;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
class GFG {
private static final String regularExpression
= "(.*)(\\d+)(.*)" ;
private static final String input
= "Hello World!, 41346, these are the numbers." ;
public static void main(String[] args)
{
Pattern myPattern
= Pattern.compile(regularExpression);
Matcher myMatcher = myPattern.matcher(input);
if (myMatcher.find()) {
MatchResult myResult
= myMatcher.toMatchResult();
System.out.println(
"Second Capturing Group - Match String: "
+ myResult.group( 1 ));
}
}
}
|
Output
Second Capturing Group - Match String: Hello World!, 4134
7. groupCount(): This method specifies the number of capturing groups in the match result’s pattern.
Syntax:
int groupCount()
Return Type:
- integer: It represents the number of capturing groups in the matcher’s pattern.
Example:
Java
import java.io.*;
import java.util.regex.MatchResult;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
class GFG {
private static final String regularExpression
= "(.*)(\\d+)(.*)" ;
private static final String input
= "Hello World!, 41346, these are the numbers." ;
public static void main(String[] args)
{
Pattern myPattern
= Pattern.compile(regularExpression);
Matcher myMatcher = myPattern.matcher(input);
if (myMatcher.find()) {
MatchResult myResult
= myMatcher.toMatchResult();
System.out.println(
"The number of capturing groups is equal to: "
+ myResult.groupCount());
}
}
}
|
Output
The number of capturing groups is equal to: 3
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...