案例:
Pattern p = Pattern.compile("a*b"); // 生成Pattern实例(设置匹配模式【规则】)- 静态方法 Matcher m = p.matcher("aaaaab"); // 生成Match实例(设置匹配选手【报名】) boolean b = m.matches(); // 匹配选手按匹配模式进行匹配操作【全部】-属简单用法 等价于: boolean b = Pattern.matches("a*b", "aaaaab"); // 静态方法 -属简单用法 总结: Pattern.compile(regex).matcher(input).matches() // 案例的链式写法 -属简单用法 等价于: Pattern.matches(regex, input); // 静态方法-属简单用法 【两个层面】:一、Pattern层 ——> 靠Pattern.compile()方法生成 p.pattern():获得匹配模式【规则】(正则表达式)的字符串形式 p.split(input):用p匹配模式分割input字符串,返回字符串数组 简单用法:Pattern.matches(regex, input); // 静态方法二、Matcher层 ——> 依赖于Pattern层而存在,靠p.matcher()方法生成 m.pattern():获得匹配模式【规则】(正则表达式)的字符串形式 m.matches():对匹配选手(整个字符串)进行完全匹配[简单用法] ——> 返回布尔值 【当匹配失败时:①若开头部分能匹配成功,指针将移动至开头匹配的结尾,但不能获取结果;②若开头部分也匹配失败,则不移动索引指针】 m.lookingAt():始终只对匹配选手开头部分的字符串进行匹配 ——> 返回布尔值 【若开头部分能匹配成功,指针将移动至开头匹配的开始,且可以获取结果;若开头部分匹配失败,则不移动索引指针】 m.find():对字符串按索引指针位置进行迭代匹配 ——> 返回布尔值 【每次匹配成功都会移动索引指针至最近一次匹配成功的开头,能拿到匹配结果;若匹配失败,则不移动索引指针】 m.start():返回最近一次匹配的初始索引 m.end():返回最近一次匹配的结束索引 m.group():返回最近一次匹配的子字符串 如果有分组,那么: m.groupCount():返回p匹配模式中的分组总数 m.start(i):返回最近一次匹配的第i个分组的初始索引 m.end(i):返回最近一次匹配的第i个分组的结束索引 m.group(i):返回最近一次匹配的第i个分组的子字符串举例如下:
System.out.println("加载配置信息");
System.out.println("通过加载配置信息加载一个代理工厂Map:"); System.out.println("这个Map存放的是接口Class与对应的代理工厂"); MyInterface myInterface = new SqlSession().getMapper(MyInterface.class); List<Object> list = myInterface.query(new Object()); System.out.println(list.size()); Pattern p = Pattern.compile("\\d+"); System.out.println(p.pattern()); System.out.println(p.toString()); System.out.println(p.flags()); System.out.println("++++++++++++++++++++++"); System.out.println(p.split("a3b896cd3265ef123g456hh789987gg456ee").length); System.out.println(p.split("a3b896cd3265ef123g456hh789987gg456ee")[0]); System.out.println(p.split("a3b896cd3265ef123g456hh789987gg456ee")[1]); System.out.println(p.split("a3b896cd3265ef123g456hh789987gg456ee")[2]); System.out.println(p.split("a3b896cd3265ef123g456hh789987gg456ee")[3]); System.out.println(p.split("a3b896cd3265ef123g456hh789987gg456ee")[4]); System.out.println(p.split("a3b896cd3265ef123g456hh789987gg456ee")[5]); System.out.println(p.split("a3b896cd3265ef123g456hh789987gg456ee")[6]); System.out.println(p.split("a3b896cd3265ef123g456hh789987gg456ee")[7]);// System.out.println(p.split("a3b896cd3265ef123g456hh789987gg456ee")[8]); System.out.println("---------------"); System.out.println(Pattern.matches("\\d+", "2223")); // true System.out.println(Pattern.matches("\\d+", "2223aa")); // false System.out.println(Pattern.matches("\\d+", "22bb23")); // false Pattern p2 = Pattern.compile("\\d+"); Matcher m2 = p2.matcher("22bb23aa56ff78hh97gg65kk98pp88"); System.out.println(m2.pattern()); // \d+ System.out.println(m2.toString()); // java.util.regex.Matcher[pattern=\d+ region=0,6 lastmatch=] System.out.println(m2.regionStart()); // 0 System.out.println(m2.regionEnd()); // 18 System.out.println("=============+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++"); System.out.println(m2.matches()); // false// System.out.println(m2.start());// System.out.println(m2.end());// System.out.println(m2.group()); System.out.println(m2.find()); // true System.out.println(m2.start()); // 4 System.out.println(m2.end()); // 6 System.out.println(m2.group()); // 23 System.out.println(m2.find()); // true System.out.println(m2.start()); // 8 System.out.println(m2.end()); // 10 System.out.println(m2.group()); // 56 System.out.println(m2.find()); // true System.out.println(m2.start()); // 12 System.out.println(m2.end()); // 14 System.out.println(m2.group()); // 78 System.out.println(m2.matches()); // false 由于 开头部分匹配,指针将移动至开头,但不能获取结果 System.out.println(m2.lookingAt()); // true 由于 开头部分匹配,指针将移动至开头,但能获取结果// System.out.println(m2.start()); // 0// System.out.println(m2.end()); // 2// System.out.println(m2.group()); // 22 System.out.println(m2.find()); // true 再次移动索引指针 System.out.println(m2.start()); // 4 System.out.println(m2.end()); // 6 System.out.println(m2.group()); // 23 System.out.println(m2.find()); // true// System.out.println(m2.start()); // System.out.println(m2.end()); // System.out.println(m2.group()); System.out.println("=============>>>>>"); Pattern p3 = Pattern.compile("(\\d+)([a-z]+)"); Matcher m3 = p3.matcher("56aaba2223bb44");// System.out.println(m3.matches()); System.out.println(m3.find()); System.out.println(m3.groupCount()); System.out.println("*******"); System.out.println(m3.start()); System.out.println(m3.end()); System.out.println(m3.group()); System.out.println(m3.start(1)); System.out.println(m3.end(1)); System.out.println(m3.group(1)); System.out.println(m3.start(2)); System.out.println(m3.end(2)); System.out.println(m3.group(2)); System.out.println("/"); Pattern p4=Pattern.compile("\\d+"); Matcher m4=p4.matcher("我的QQ是:1307787223 我的电话是:18617375789 我的邮箱是:crackhu@163.com"); while(m4.find()) { System.out.println(m4.group()); System.out.print("start:"+m4.start()); System.out.println(" end:"+m4.end()); }