如何使用 Java 正則表示式匹配輸入結尾?
可以使用元字元“\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
廣告
資料結構
網路
關係型資料庫管理系統 (RDBMS)
作業系統
Java
iOS
HTML
CSS
Android
Python
C 程式設計
C++
C#
MongoDB
MySQL
JavaScript
PHP