English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

Matcher regionEnd() method and example in Java

java.util.regex.Matcher class represents the engine for executing various matching operations. This class has no constructor, and can be usedmatches()method of java.util.regex.Pattern creates/get an object of this class.

This class (Matcher)regionEnd()The method returns an integer value that represents the end index of the current matcher object.

example1

import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class RegionEndExample {
   public static void main(String[] args) {
      String regex = "(.*)\\d+)";*)";
      String input = "This is a sample Text, 1234, with numbers in between.";
      //Create a pattern object
      Pattern pattern = Pattern.compile(regex);
      //Create a Matcher object
      Matcher matcher = pattern.matcher(input);
      //set the region of the matcher
      matcher.region(5, 20);
      if(matcher.matches()) {
         System.out.println("Match found");
      } else {
         System.out.println("Match not found");
      }
      System.out.print("End of the region: ",+matcher.regionEnd());
   }
}

Output Result

Match not found
End of the region: 20

example2

import java.util.Scanner;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class RegionEndExample {
   public static void main(String[] args) {
      //The regular expression can accept6to10a character
      String regex = "[#]";
      System.out.println("Enter a string:");
      Scanner sc = new Scanner(System.in);
      String input = sc.nextLine();
      //Create a pattern object
      Pattern pattern = Pattern.compile(regex);
      //Create a Matcher object
      Matcher matcher = pattern.matcher(input);
      //Set the region to the input string
      matcher.region(2, 4);
      //Switch to transparent range
      if(matcher.find()) {
         System.out.println("Match found");
      } else {
         System.out.println("Match not found");
      }
      System.out.println("Ending of the region: "+ matcher.regionEnd());
   }
}

Output Result

Enter a string:
this is sample text #
Match not found
Ending of the region: 4