Computer >> 컴퓨터 >  >> 프로그램 작성 >> Java

Java RegEx를 사용하여 입력의 끝을 일치시키는 방법은 무엇입니까?

<시간/>

메타 문자 "\\z"를 사용하여 입력의 끝을 일치시킬 수 있습니다.

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 ");
      }
   }
}

출력 1

Enter a String
sample text
Match not found

출력 2

Enter a String
sample text 23
Match found