Java中的模式pattern方法及示例
java的java.util.regex包提供了各种类来查找字符序列中的特定模式。该程序包的模式类是正则表达式的编译表示。
Pattern类的pattern()方法以字符串格式获取并返回正则表达式,使用该正则表达式编译当前模式。
例子1
输出结果
Date is valid
Regular expression: ^(1[0-2]|0[1-9])/(3[01]|[12][0-9]|0[1-9])/[0-9]{4}$例子2
public class PatternExample {
public static void main(String[] args) {
String input = "Hi my id is 056E1563";
//使用组的正则表达式
String regex = "(.*)?(\\d+)";
//创建一个模式对象
Pattern pattern = Pattern.compile(regex);
if(pattern.matcher(input).matches()) {
System.out.println("Match found");
} else {
System.out.println("Match not found");
}
//检索当前模式的正则表达式
String regularExpression = pattern.pattern();
System.out.println("Regular expression: "+regularExpression);
}
}输出结果
Match found Regular expression: (.*)?(\d+)
热门推荐
10 新年门口花束祝福语简短
11 盘锦结婚祝福语大全简短
12 父母生日祝福语 简短独特
13 家庭恩爱祝福语简短英文
14 高考俄语祝福语大全简短
15 虎年祝福语 诗句唯美简短
16 生日婚礼祝福语简短精辟
17 虎年喝酒拜年祝福语简短
18 教师闺蜜祝福语简短