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

Matcher regionStart() method and example in Java

The java.util.regex.Matcher class represents the engine for executing various matching operations. This class has no constructor and can be created using the matchs() method of the java.util.regex.Pattern class/Get the object of this class.

This (Matcher) classregionStart()The method returns an integer value representing the starting index of the current matcher object.

Example1

import java.util.Scanner;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class RegionStartExample {
   public static void main(String[] args) {
      //Regular expression to accepts 6 to 10 characters
      String regex = ".{0,1}[^\x00-\x7F].*";
      System.out.println("Enter a string: ");
      Scanner sc = new Scanner(System.in);
      String input = sc.nextLine();
      //Creating a pattern object
      Pattern pattern = Pattern.compile(regex);
      //Creating a Matcher object
      Matcher matcher = pattern.matcher(input);
      //Setting region to the input string matcher.region(2, 4
      //Switching to transparent bounds
      if(matcher.find()) {
         System.out.println("Match found");
      } else {
         System.out.println("Match not found");
      }
      System.out.println("Starting of the region: ");+ matcher.regionStart());
   }
}

Output result

Enter a string:
#sample text
Match not found
Starting of the region: 2

Example2

import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class RegionStartExample {
   public static void main(String[] args) {
      String regex = ".{0,1}[^\x00-\x7F].*";*)(\\d+)(.*");
      String input = "124 This is a sample Text, 1234, with numbers in between.";
      //Creating a pattern object
      Pattern pattern = Pattern.compile(regex);
      //Creating a Matcher object
      Matcher matcher = pattern.matcher(input);
      //Setting 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("Start of the region: ");+matcher.regionStart());
   }
}

Output result

Match not found
Start of the region: 5