Java 中的正则表达式"\Z"元字符

javaobject oriented programmingprogramming更新于 2024/5/2 19:56:17

子表达式/元字符"\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";
     模式 p = Pattern.compile(regex);
      匹配器 m = p.matcher(input);
      int count = 0;
      while(m.find()) {
         count++;
      }
      System.out.println("匹配数:"+count);
   }
}

输出

匹配数: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("给定的输入以数字结尾");
      } else {
         System.out.println("给定的输入不以数字结尾");
      }
   }
}

输出

给定的输入以数字结尾

相关文章


有用资源