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

Matcher requireEnd() method with example in Java

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

If the match is successful, this (Matcher) classrequireEnd()The method checks if there is an opportunity for the matching result to be false (if there is more input), and if so, this method returns true, otherwise 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 might match, but if you accept more sentences with new lines, 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.

Example1

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();
      //Instantiate Pattern class
      Pattern pattern = Pattern.compile(regex);
      //Instantiate Matcher class
      Matcher matcher = pattern.matcher(input);
      //Verify whether a match occurs
      if(matcher.find()) {
         System.out.println("Match found");
      }
      boolean result = matcher.requireEnd();
      if(result) {
         System.out.println("More input may turn the result of the match false");
      } else{
         System.out.println("The result of the match will be true, despite more data");
      }
   }
}

Output result

Enter input text:
Hello how are you
Match found
More input may turn the result of the match false

Example2

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();
      //Instantiate Pattern class
      Pattern pattern = Pattern.compile(regex);
      //Instantiate Matcher class
      Matcher matcher = pattern.matcher(input);
      //Verify whether a match occurs
      if(matcher.find()) {
         System.out.println("Match found");
      }
      boolean result = matcher.requireEnd();
      if(result) {
         System.out.println("More input may turn the result of the match false");
      } else{
         System.out.println("The result of the match will be true, despite more data");
      }
   }
}

Output result

Enter input text:
Hello# how# are you
Match found
The result of the match will be true, despite more data