

- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Working with Matcher.start() method in Java Regular Expressions
The start index of the subsequence that was found by the group during the previous match operation is returned by the method java.util.regex.Matcher.start(). This method has a single argument i.e.the capturing group’s index for the specified pattern.
A program that demonstrates the method Matcher.start() Java regular expressions is given as follows:
Example
import java.util.regex.Matcher; import java.util.regex.Pattern; public class Demo { public static void main(String args[]) { Pattern p = Pattern.compile("(a*b)"); Matcher m = p.matcher("caaabccaab"); System.out.println("The input string is: caaabccaab"); System.out.println("The Regex is: (a*b)"); System.out.println(); while (m.find()) { System.out.println("Index: " + m.start(1)); } } }
Output
The input string is: caaabccaab The Regex is: (a*b) Index: 1 Index: 7
Now let us understand the above program.
The subsequence “(a*b)” is searched in the string sequence "caaabccaab". The find() method is used to find if the subsequence is in the input sequence and the start index of the subsequence that was found by the group during the previous match operation is printed using the start() method. A code snippet which demonstrates this is as follows:
Pattern p = Pattern.compile("(a*b)"); Matcher m = p.matcher("caaabccaab"); System.out.println("The input string is: caaabccaab"); System.out.println("The Regex is: (a*b)"); System.out.println(); while(m.find()) { System.out.println("Index: " + m.start(1)); }
- Related Questions & Answers
- Working with Matcher.end() method in Java Regular Expressions
- Working with simple groups in Java Regular Expressions
- Matcher.pattern() method in Java Regular Expressions
- Pattern.matches() method in Java Regular Expressions
- Validate Phone with Java Regular Expressions
- Role of Matcher.matches() method in Java Regular Expressions
- Role of Matcher.group() method in Java Regular Expressions
- Java Regular Expressions Tutorial
- Search and Replace with Java regular expressions
- Role of Matcher.find(int) method in Java Regular Expressions
- Validate city and state with Java Regular Expressions
- Validate the ZIP code with Java Regular expressions
- Java regular expressions sample examples
- Possessive quantifiers Java Regular expressions
- Java Regular expressions Logical operators
Advertisements