Skip to content
Related Articles
Open in App
Not now

Related Articles

MatchResult Interface in Java

Improve Article
Save Article
  • Last Updated : 18 Feb, 2022
Improve Article
Save Article

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




// Java program to illustrate the working of start() method
 
import java.io.*;
import java.util.regex.MatchResult;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
 
class GFG {
 
    // Initializing regular expression string
    private static final String regularExpression
        = "(.*)(\\d+)(.*)";
 
    // Initializing input string
    private static final String input
        = "Hello World!, 41346, these are the numbers.";
 
    // Main method
    public static void main(String[] args)
    {
        // Compiling the given regular expression string
        Pattern myPattern
            = Pattern.compile(regularExpression);
 
        // Applying matcher operation
        Matcher myMatcher = myPattern.matcher(input);
 
        if (myMatcher.find()) {
 
            // Retrieve the MatchResult Object
            MatchResult myResult
                = myMatcher.toMatchResult();
 
            // Printing the starting index of the match
            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




// Java program to illustrate the working
// of start(int index) method
 
import java.util.regex.MatchResult;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
 
public class GFG {
 
    // Initializing the regular expression string
    private static final String regularExpression
        = "(.*)(\\d+)(.*)";
 
    // Initializing the input string
    private static final String input
        = "Hello World!, 41346, these are the numbers.";
 
    // Main method
    public static void main(String[] args)
    {
        // Compiling the regular expression string
        Pattern myPattern
            = Pattern.compile(regularExpression);
 
        // Applying matcher operation
        Matcher myMatcher = myPattern.matcher(input);
 
        if (myMatcher.find()) {
 
            // Retrieve the MatchResult Object
            MatchResult myResult
                = myMatcher.toMatchResult();
 
            // Prints the start index of the sequence
            // that was held by the given group during the
            // match.
            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




// Java program to illustrate the working of end() method
 
import java.io.*;
import java.util.regex.MatchResult;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
 
class GFG {
 
    // Initializing the regular expression string
    private static final String regularExpression
        = "(.*)(\\d+)(.*)";
 
    // Initializing the input string
    private static final String input
        = "Hello World!, 41346, these are the numbers.";
 
    // Main method
    public static void main(String[] args)
    {
        // Compiling the regular expression string
        Pattern myPattern
            = Pattern.compile(regularExpression);
 
        // Applying the matcher operation
        Matcher myMatcher = myPattern.matcher(input);
 
        if (myMatcher.find()) {
            // Retrieve the MatchResult object
            MatchResult myResult
                = myMatcher.toMatchResult();
 
            // Prints the offset following the last
            // character matched
            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




// Java program to illustrate the working
// of end(int index) method
 
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)
    {
        // Compiling regular expression string
        Pattern myPattern
            = Pattern.compile(regularExpression);
 
        // applying matcher operation
        Matcher myMatcher = myPattern.matcher(input);
 
        if (myMatcher.find()) {
            // get the MatchResult Object
            MatchResult myResult
                = myMatcher.toMatchResult();
 
            // Prints the offset after the last
            // character of the sequence held by
            // the given group at the time of match.
            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




// Java program to illustrate the working of group() method
 
import java.io.*;
import java.util.regex.MatchResult;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
 
class GFG {
 
    // Initializing regular expression string
    private static final String regularExpression
        = "(.*)(\\d+)(.*)";
 
    // Initializing input string
    private static final String input
        = "Hello World!, 41346, these are the numbers.";
 
    // Main() method
    public static void main(String[] args)
    {
        // Compiling regular expression
        Pattern myPattern
            = Pattern.compile(regularExpression);
 
        // Initiating the matcher object
        Matcher myMatcher = myPattern.matcher(input);
 
        if (myMatcher.find()) {
 
            // Initiating the MatchResult object
            MatchResult myResult
                = myMatcher.toMatchResult();
 
            // Prints the input sequence matched by the
            // previous match.
            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




// Java program to illustrate the working
// of group(int index) method
 
import java.io.*;
import java.util.regex.MatchResult;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
 
class GFG {
 
    // Initializing regular expression string
    private static final String regularExpression
        = "(.*)(\\d+)(.*)";
 
    // Initializing input string
    private static final String input
        = "Hello World!, 41346, these are the numbers.";
 
    // Main method
    public static void main(String[] args)
    {
        // Compiling regular expression
        Pattern myPattern
            = Pattern.compile(regularExpression);
 
        // Instantiating matcher object
        Matcher myMatcher = myPattern.matcher(input);
 
        if (myMatcher.find()) {
            // Instantiating MatchResult object
            MatchResult myResult
                = myMatcher.toMatchResult();
 
            // Prints the input subsequence that was held
            // by the given group during the last match
            // occurred.
            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




// Java program to illustrate the working
// of groupCount() method
 
import java.io.*;
import java.util.regex.MatchResult;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
 
class GFG {
 
    // Regular expression
    private static final String regularExpression
        = "(.*)(\\d+)(.*)";
 
    // Input string
    private static final String input
        = "Hello World!, 41346, these are the numbers.";
 
    public static void main(String[] args)
    {
        // Compiling regular expression
        Pattern myPattern
            = Pattern.compile(regularExpression);
 
        // Instantiating a matcher object
        Matcher myMatcher = myPattern.matcher(input);
 
        if (myMatcher.find()) {
            // Instantiating a MatchResult Object
            MatchResult myResult
                = myMatcher.toMatchResult();
 
            // Prints the number of capturing groups in this
            // match result's pattern.
            System.out.println(
                "The number of capturing groups is equal to: "
                + myResult.groupCount());
        }
    }
}

Output

The number of capturing groups is equal to: 3

My Personal Notes arrow_drop_up
Related Articles

Start Your Coding Journey Now!