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 = "Nhooo\\z";
String input = "Hi how are you welcome to Nhooo";
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
以下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");
}
}
}输出结果
Given input ends with a digit
热门推荐
4 简短平安夜祝福语
10 如何求婚文案祝福语简短
11 职校离校祝福语大全简短
12 鼠年春节祝福语简短创意
13 姐姐结婚祝福语简短汇总
14 写给同事生日祝福语简短
15 新年情人祝福语大全 简短
16 同事姐妹离别祝福语简短
17 新居致喜祝福语简短
18 婚礼闺蜜祝福语简短