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

Метод regionEnd() в Java Matcher с примерами

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

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

Example 1

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.";
      //Создать объект шаблона
      Pattern pattern = Pattern.compile(regex);
      //Создать объект Matcher
      Matcher matcher = pattern.matcher(input);
      // Set the region of the matcher
      matcher.region(5, 20);
      if(matcher.matches()) {
         System.out.println("Совпадение найдено");
      } else {
         System.out.println("Совпадение не найдено");
      }
      System.out.print("End of the region: "+matcher.regionEnd());
   }
}

Результат вывода

Совпадение не найдено
End of the region: 20

Example 2

import java.util.Scanner;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class RegionEndExample {
   public static void main(String[] args) {
      // Regular expression accepts 6 to 10 characters
      String regex = "[#]";
      System.out.println("Enter a string: ");
      Scanner sc = new Scanner(System.in);
      String input = sc.nextLine();
      //Создать объект шаблона
      Pattern pattern = Pattern.compile(regex);
      //Создать объект Matcher
      Matcher matcher = pattern.matcher(input);
      //Установить область на входную строку
      matcher.region(2, 4);
      //Перейти к прозрачной области
      if(matcher.find()) {
         System.out.println("Совпадение найдено");
      } else {
         System.out.println("Совпадение не найдено");
      }
      System.out.println("Конец региона: " + matcher.regionEnd());
   }
}

Результат вывода

Введите строку:
Это примерный текст #
Совпадение не найдено
Конец региона: 4
Основной учебник
Вам может понравиться