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

Java의 정규식 \Z 메타 문자

<시간/>

하위 표현식/메타 문자 "\Z"는 허용되는 마지막 줄 종결자를 제외한 전체 문자열의 끝과 일치합니다.

예시 1

import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class RegexExample {
   public static void main( String args[] ) {
      String regex = "Tutorialspoint\\z";
      String input = "Hi how are you welcome to Tutorialspoint";
      Pattern p = Pattern.compile(regex);
      Matcher m = p.matcher(input);
      int count = 0;
      while(m.find()) {
         count++;
      }
      System.out.println("Number of matches: "+count);
   }
}

출력

Number of matches: 1

예시 2

다음 자바 프로그램은 주어진 입력 텍스트가 숫자로 끝나는지 확인합니다.

import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Data {
   public static void main( String args[] ) {
      String regex = "[0-9]\\z";
      String input = "Hi how are you \n this is sample text \n this is third line 554";
      Pattern p = Pattern.compile(regex);
      Matcher m = p.matcher(input);
      if(m.find()) {
         System.out.println("Given input ends with a digit");
      } else {
         System.out.println("Given input doesn’t end with a digit");
      }
   }
}

출력

Given input ends with a digit