The start(int group) method of the Matcher class returns the starting index of the subsequence captured by the specified capturing group during the most recent successful match. It is useful when a regular expression contains multiple capturing groups and you want to determine where a particular group begins in the input string.
- Returns the starting index of the specified capturing group.
- Works only after a successful match using methods like find() or matches().
- Commonly used to locate the position of matched groups in text processing.
import java.util.regex.*;
public class Geeks {
public static void main(String[] args) {
Pattern pattern = Pattern.compile("(Java)");
Matcher matcher = pattern.matcher("Learn Java Programming");
if (matcher.find()) {
System.out.println("Start Index: "
+ matcher.start(1));
}
}
}
Output
Start Index: 6
Explanation: The regular expression (Java) creates one capturing group. After find() locates the word "Java", the start(1) method returns the starting index of the first capturing group, which is 6.
Syntax
public int start(int group)
Parameter
- group: The index of the capturing group whose starting position is required.
Return Value
- Returns the starting index of the specified capturing group in the input sequence.
Exceptions
- IllegalStateException: If no successful match has been performed.
- IndexOutOfBoundsException: If the specified group number does not exist.
Example: Getting the Start Index of a Capturing Group
import java.util.regex.*;
public class Geeks {
public static void main(String[] args) {
Pattern pattern = Pattern.compile("(G*s)");
Matcher matcher = pattern.matcher("GeeksForGeeks");
while (matcher.find()) {
System.out.println("Start Index: "
+ matcher.start(1));
}
}
}
Output
Start Index: 4 Start Index: 12
Explanation: The regular expression (G*s) matches zero or more 'G' characters followed by 's'. In the string "GeeksForGeeks", the pattern matches the character 's' in both "Geeks" words. The start(1) method returns the starting index of the first capturing group for each match, which are 4 and 12.
Example: Getting the Start Index of the Entire Match
import java.util.regex.*;
public class Geeks {
public static void main(String[] args) {
Pattern pattern = Pattern.compile("(G*G)");
Matcher matcher = pattern.matcher("GFGFGFGFGFGFGFGFGFG");
while (matcher.find()) {
System.out.println("Start Index: "
+ matcher.start(0));
}
}
}
Output
Start Index: 0 Start Index: 2 Start Index: 4 Start Index: 6 Start Index: 8 Start Index: 10 Start Index: 12 Start Index: 14 Start Index: 16 Start Index: 18
Explanation: The regular expression (G*G) matches sequences ending with the character 'G'. Here, start(0) returns the starting index of the entire matched pattern (group 0). Since matches occur at every alternate position, the starting indices are 0, 2, 4, ..., 18.
Difference between start() and start(int)
| Method | Description |
|---|---|
| start() | Returns the starting index of the entire match. |
| start(0) | Also returns the starting index of the entire match. |
| start(int group) | Returns the starting index of the specified capturing group. |
Advantages of start(int) Method
- Returns the starting position of a specific capturing group.
- Useful when working with complex regular expressions containing multiple groups.
- Helps locate matched data within the input string.
- Works seamlessly with find(), matches(), and capturing groups.
- Simplifies parsing and extracting structured text using regular expressions.