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

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

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

If matched, this (Matcher) classrequireEnd()The method checks if there is an opportunity for a false matching result (if there is more input), and if so, this method returns true, otherwise it returns false.

For example, if you try to match the last word of the input string with the regular expression "you $" and if your first input line is "hello you are", you may match, but if you accept more sentences, the last word of the new line may not be the required word (i.e., "you"), making the matching result false. In this case, therequiredEnd()The method returns true.

Similarly, if you try to match a specific character in the input, say #, and if your first input line is "Hello#How are you", you will have a match. More input data may change the content of the matcher, but will not change the result, this is true. In this case, therequiredEnd()The method returns false.

Example 1

import java.util.Scanner;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class RequiredEndExample {
   public static void main(String args[]) {
      String regex = "you$";
      //Read user input
      Scanner sc = new Scanner(System.in);
      System.out.println("Enter input text:");
      String input = sc.nextLine();
      //Инстанцирование класса Pattern
      Pattern pattern = Pattern.compile(regex);
      //Инстанцирование класса Matcher
      Matcher matcher = pattern.matcher(input);
      //Проверка на произошедшее совпадение
      if(matcher.find()) {
         System.out.println("Найден матч");
      }
      boolean result = matcher.requireEnd();
      if(result) {
         System.out.println("Более ввода может сделать результат матча ложно");
      } else{
         System.out.println("Результат матча будет истинным, несмотря на наличие более данных");
      }
   }
}

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

Введите текст ввода:
Hello, how are you
Найден матч
More input may turn the result of the match false

Example 2

import java.util.Scanner;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class RequiredEndExample {
   public static void main(String args[]) {
      String regex = "[#]";
      //Read user input
      Scanner sc = new Scanner(System.in);
      System.out.println("Enter input text:");
      String input = sc.nextLine();
      //Инстанцирование класса Pattern
      Pattern pattern = Pattern.compile(regex);
      //Инстанцирование класса Matcher
      Matcher matcher = pattern.matcher(input);
      //Проверка на произошедшее совпадение
      if(matcher.find()) {
         System.out.println("Найден матч");
      }
      boolean result = matcher.requireEnd();
      if(result) {
         System.out.println("Более ввода может сделать результат матча ложно");
      } else{
         System.out.println("Результат матча будет истинным, несмотря на наличие более данных");
      }
   }
}

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

Введите текст ввода:
Привет# как# ты
Найден матч
Результат матча будет истинным, несмотря на наличие более данных
Рекомендуем также