Open In App

Matcher start(int) method in Java with Examples

Last Updated : 26 Nov, 2018
Improve
Improve
Like Article
Like
Save
Share
Report

The start(int group) method of Matcher Class is used to get the start index of the match result already done, from the specified group.

Syntax:

public int start(int group)

Parameters: This method takes a parameter group which is the group from which the start index of the matched pattern is required.

Return Value: This method returns the index of the first character matched from the specified group.

Exception: This method throws:

  • IllegalStateException if no match has yet been attempted, or if the previous match operation failed.
  • IndexOutOfBoundsException if there is no capturing group in the pattern with the given index.

Below examples illustrate the Matcher.start() method:

Example 1:




// Java code to illustrate start() method
  
import java.util.regex.*;
  
public class GFG {
    public static void main(String[] args)
    {
  
        // Get the regex to be checked
        String regex = "(G*s)";
  
        // Create a pattern from regex
        Pattern pattern
            = Pattern.compile(regex);
  
        // Get the String to be matched
        String stringToBeMatched
            = "GeeksForGeeks";
  
        // Create a matcher for the input String
        Matcher matcher
            = pattern
                  .matcher(stringToBeMatched);
  
        // Get the current matcher state
        MatchResult result
            = matcher.toMatchResult();
        System.out.println("Current Matcher: "
                           + result);
  
        while (matcher.find()) {
            // Get the first index of match result
            System.out.println(matcher.start(1));
        }
    }
}


Output:

Current Matcher: java.util.regex.Matcher[pattern=(G*s) region=0,13 lastmatch=]
4
12

Example 2:




// Java code to illustrate start() method
  
import java.util.regex.*;
  
public class GFG {
    public static void main(String[] args)
    {
  
        // Get the regex to be checked
        String regex = "(G*G)";
  
        // Create a pattern from regex
        Pattern pattern
            = Pattern.compile(regex);
  
        // Get the String to be matched
        String stringToBeMatched
            = "GFGFGFGFGFGFGFGFGFG";
  
        // Create a matcher for the input String
        Matcher matcher
            = pattern
                  .matcher(stringToBeMatched);
  
        // Get the current matcher state
        MatchResult result
            = matcher.toMatchResult();
        System.out.println("Current Matcher: "
                           + result);
  
        while (matcher.find()) {
            // Get the first index of match result
            System.out.println(matcher.start(0));
        }
    }
}


Output:

Current Matcher: java.util.regex.Matcher[pattern=(G*G) region=0,19 lastmatch=]
0
2
4
6
8
10
12
14
16
18

Reference: Oracle Doc



Similar Reads

TimeZone getOffset(int, int, int, int, int, int) Method in Java with Examples
The getOffset(int era, int yr, int mon, int day, int dayOfWeek, int millisec) method of TimeZone class in Java is used to know the offset value of this TimeZone at a specific date or modified date in case of daylight savings, from the UTC or the Universal Time Coordinated. This offset value can be added to get the local time. Syntax: public abstrac
2 min read
Matcher region(int, int) method in Java with Examples
The region(int, int) method of Matcher Class restricts the region to be matched by the pattern. This region must be lesser than or same as the previous region, but not greater. Else it would lead to IndexOutOfBoundsException. This method returns a Matcher with the new matching region. Syntax: public Matcher region(int startIndex, int endIndex) Para
3 min read
ZoneOffset ofHoursMinutesSeconds(int, int, int) method in Java with Examples
The ofHoursMinutesSeconds(int, int, int) method of ZoneOffset Class in java.time package is used to obtain an instance of ZoneOffset using the offset in hours, minutes and seconds passed as the parameter. This method takes the hours, minutes and seconds as parameter in the form of int and converts it into the ZoneOffset. Syntax: public static ZoneO
2 min read
SimpleTimeZone setStartRule(int, int, int) method in Java with Examples
The setStartRule(int startofMonth, int startofDay, int startofTime) method of SimpleTimeZone class in Java is used to set a particular start rule of the day-light saving time to a fixed date within a given month. Syntax: public void setStartRule(int startofMonth, int startofDay, int startofTime) Parameters: The method takes three parameters: starto
2 min read
SimpleTimeZone setEndRule(int, int, int) method in Java with Examples
The setEndRule(int endofMonth, int endofDay, int endofTime) method of SimpleTimeZone class in Java is used to set a particular rule of the day-light saving time to a fixed date within a given month. Syntax: public void setEndRule(int endofMonth, int endofDay, int endofTime) Parameters: The method takes three parameters: endofMonth: This is of Integ
2 min read
Matcher end(int) method in Java with Examples
The end(int group) method of Matcher Class is used to get the offset after the end index of the match result already done, from the specified group. Syntax: public int end(int group) Parameters: This method takes a parameter group from which the offset after the end index of the matched pattern is required. Return Value: This method returns the off
2 min read
Matcher find(int) method in Java with Examples
The find(int start) method of Matcher Class attempts to find the next subsequence after the specified subsequence number, passed as parameter, of the input sequence that find the pattern. It returns a boolean value showing the same. Syntax: public boolean find(int start) Parameters: This method takes a parameter start which is the subsequence numbe
2 min read
Matcher group(int) method in Java with Examples
The group(int group) method of Matcher Class is used to get the group index of the match result already done, from the specified group. Syntax: public String group(int group) Parameters: This method takes a parameter group which is the group from which the group index of the matched pattern is required. Return Value: This method returns the index o
2 min read
Matcher start() method in Java with Examples
The start() method of Matcher Class is used to get the start index of the match result already done. Syntax: public int start() Parameters: This method do not takes any parameter. Return Value: This method returns the index of the first character matched.0 Exception: This method throws IllegalStateException if no match has yet been attempted, or if
2 min read
Matcher start(String) method in Java with Examples
The start(String string) method of Matcher Class is used to get the start index of the match result already done, from the specified string. Syntax: public int start(String string) Parameters: This method takes a parameter string which is the String from which the start index of the matched pattern is required. Return Value: This method returns the
2 min read
Practice Tags :