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 婆婆高寿祝福语大全简短