java matcher类的概念简单总结

来源:互联网 发布:linux echo 颜色 编辑:程序博客网 时间:2024/04/29 09:49


重要概念介绍!!!:

1 组(group):matches,find,lookingAt三个函数的调用会产生组(group),表示一个成功匹配的部分

2 区域(region):通过region(int start,int end)函数设置区域的边界,同时还有 useTransparentBounds函数与useTransparentBounds函数设置边界的属性。

3 三个主要查找函数的区别:

          1) matches 将整个区域试图与pattern匹配,如果成功返回true,并且产生一个group,为整个区域。

          2)lookingAt 从区域的开头与pattern匹配,return true if, and only if, a prefix of the input sequence matches this matcher's pattern。不要求整个区域都匹配,只要前缀匹配即可。

         3) find()用扫描的方式查找整个区域,直到找到一个匹配的子串,例如,下列代码讲所有的cat换成dog。

Pattern p = Pattern.compile("cat"); Matcher m = p.matcher("one cat two cats in the yard"); StringBuffer sb = new StringBuffer(); while (m.find()) {     m.appendReplacement(sb, "dog"); } m.appendTail(sb); System.out.println(sb.toString());

0 0