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

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 = "w3codebox\\z";
      String input = "Hi how are you welcome to the3codebox";
      Pattern p = Pattern.compile(regex);
      Matcher m = p.matcher(input);
      int count = 0;
      while(m.find()) {
         count++;
      }
      System.out.println("Number of matches: ");+count);
   }
}

Output result

Number of matches: 1

例2

以下Javaプログラムは、与えられた入力テキストが数字で終わるかどうかを確認します。

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

Output result

Given input ends with a digit