
- Java Tutorial
- Java - Home
- Java - Overview
- Java - Environment Setup
- Java - Basic Syntax
- Java - Object & Classes
- Java - Constructors
- Java - Basic Datatypes
- Java - Variable Types
- Java - Modifier Types
- Java - Basic Operators
- Java - Loop Control
- Java - Decision Making
- Java - Numbers
- Java - Characters
- Java - Strings
- Java - Arrays
- Java - Date & Time
- Java - Regular Expressions
- Java - Methods
- Java - Files and I/O
- Java - Exceptions
- Java - Inner classes
- Java Object Oriented
- Java - Inheritance
- Java - Overriding
- Java - Polymorphism
- Java - Abstraction
- Java - Encapsulation
- Java - Interfaces
- Java - Packages
- Java Advanced
- Java - Data Structures
- Java - Collections
- Java - Generics
- Java - Serialization
- Java - Networking
- Java - Sending Email
- Java - Multithreading
- Java - Applet Basics
- Java - Documentation
- Java Useful Resources
- Java - Questions and Answers
- Java - Quick Guide
- Java - Useful Resources
- Java - Discussion
- Java - Examples
Matcher start() method in Java with Examples
The java.util.regex.Matcher class represents an engine that performs various match operations. There is no constructor for this class you can create/obtain an object of this class using the matches() method of the class java.util.regex.Pattern.
The start() method of the Matcher class returns the starting index of the matched character.
Example
The subexpression "[...]" matches the characters specified within the braces in the input string, In the following example using this to match the character t. Here,
We have compiled the regular expression using the compile() method.
Obtained the Matcher object.
Invoked the matcher() method on each match.
Example
import java.util.Scanner; import java.util.regex.Matcher; import java.util.regex.Pattern; public class StartExample { public static void main(String[] args) { Scanner sc = new Scanner(System.in); System.out.println("Enter input text: "); String input = sc.nextLine(); String regex = "[t]"; //Creating a pattern object Pattern pattern = Pattern.compile(regex); //Matching the compiled pattern in the String Matcher matcher = pattern.matcher(input); int count =0; while (matcher.find()) { int start = matcher.start(); System.out.println(start); } } }
Output
Enter input text: Hello how are you welcome to Tutorialspoint 26 31 42
Since the character t occurred thrice in the input string, you can observe three index values (representing the index of each character).
- Related Articles
- Matcher region(int start, int end) method in Java with Examples
- Matcher matches() method in Java with Examples
- Pattern matcher() method in Java with examples
- Matcher pattern() method in Java with Examples
- Matcher replaceAll() method in Java with Examples
- Matcher replaceFirst() method in Java with Examples
- Matcher requireEnd() method in Java with Examples
- Matcher reset() method in Java with Examples
- Matcher toString() method in Java with Examples
- Matcher appendTail() method in Java with Examples
- Matcher group() method in Java with Examples
- Matcher groupCount() method in Java with Examples
- Matcher appendReplacement() method in Java with Examples
- Matcher regionEnd() method in Java with Examples
- Matcher regionStart() method in Java with Examples
