如何使用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
热门推荐
10 店家搬家祝福语大全简短
11 参加儿子大学祝福语简短
12 回国了祝福语简短英语
13 新年酒席开席祝福语简短
14 祝贺开业文案祝福语简短
15 喜得新房文案祝福语简短
16 蛋糕祝老师祝福语简短
17 班级集体生日祝福语简短
18 大寿爸爸祝福语简短英文