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 生日广州男生祝福语简短