如何使用Java RegEx匹配输入的开头?
您可以使用元字符“\\A”来匹配输入的开头。
示例
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 = "\\A[0-9]";
//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 12 sample text Match found
输出2
Enter a String sample text Match not found
热门推荐
10 爸爸爱你祝福语大全简短
11 写给老师祝福语简短大学
12 股市祝福语简短10字
13 顾客简短祝福语大全集
14 送个姐妹生日祝福语简短
15 孙女结婚姥姥祝福语简短
16 男朋友祝福语简短新年
17 国庆简短幽默祝福语
18 女朋友考试祝福语简短