
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
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
MatchResult end() Method in Java with Examples
The java.util.regex.MatcheResult interface provides methods to retrieve the results of a match.
You can get an object of this interface using the toMatchResult() method of the Matcher class. This method returns a MatchResult object which represents the match state of the current matcher.
The end() method of this interface returns the offset after the last match occurred.
Example
import java.util.Scanner; import java.util.regex.MatchResult; import java.util.regex.Matcher; import java.util.regex.Pattern; public class Example { public static void main( String args[] ) { String regex = "you$"; //Reading input from user Scanner sc = new Scanner(System.in); String input = "Hello how are you"; //Instantiating the Pattern class Pattern pattern = Pattern.compile(regex); //Instantiating the Matcher class Matcher matcher = pattern.matcher(input); //verifying whether a match occurred if(matcher.find()) { System.out.println("Match found"); } MatchResult res = matcher.toMatchResult(); int end = res.end(); System.out.println(end); } }
Output
Enter input text: hello how are you Match found 17
Advertisements