Thanks to visit codestin.com
Credit goes to www.tutorialspoint.com

Match End of Input Using Java Regex



You can match the end of the input using the meta character “\z”.

Example

import java.util.Scanner;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Example {
   public static void main(String args[]) {
      //Reading String from user
      System.out.println("Enter a String");
      Scanner sc = new Scanner(System.in);
      String input = sc.nextLine();
      String regex = "[0-9]\z";
      //Compiling the regular expression
      Pattern pattern = Pattern.compile(regex);
      //Retrieving the matcher object
      Matcher matcher = pattern.matcher(input);
      int count = 0;
      if(matcher.find()) {
         System.out.println("Match found ");
      } else {
         System.out.println("Match not found ");
      }
   }
}

Output 1

Enter a String
sample text
Match not found

Output 2

Enter a String
sample text 23
Match found
Updated on: 2019-11-19T08:04:07+05:30

526 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements