English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
java.util.regex.Matcher class represents an engine for executing various matching operations. This class has no constructor, and can be used withmatches()
created by the java.util.regex.Pattern class method/To obtain an object of this class.
The method of Matcher classend()The method returns the offset after the last match represented by the current object.
The sub-expression " [...]" matches the specified characters within the curly braces in the input string, as shown in the following example, to match the charactert.Here,
We have used thecompile()
The method compiles the regular expression.
Get the Matcher object.
matcher()
Call this method on each match item.
import java.util.Scanner; import java.util.regex.Matcher; import java.util.regex.Pattern; public class EndExample { 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]"; //Create a pattern object Pattern pattern = Pattern.compile(regex); //Match the compiled pattern in the string Matcher matcher = pattern.matcher(input); int count =0; while (matcher.find()) { int end = matcher.end(); System.out.println(end); } } }
Output Result
Enter input text: Hello how are you welcome to w3codebox 27 32 43
Since the character 't' appears three times in the input string, you can observe three offset values (representing the position in the input string after each occurrence).