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 20岁简短生日祝福语