4.3 public Matcher reset(CharSequence input)
重新设置Matcher的状态,并且将候选字符序列设置为input后进行Matcher,
这个方法和重新创建一个Matcher一样,只是这样可以重用以前的对象。
4.4 public int start()
返回Matcher所匹配的字符串在整个字符串的的开始下标。
String candidateString = "My name is Kuang. Kuang ZW.";
String matchHelper[] = {" ^", " ^"};
Pattern p = Pattern.compile("Kuang");
Matcher matcher = p.matcher(candidateString);
// Find the starting point of the first 'Bond'
matcher.find();
int startIndex = matcher.start();
System.out.println(candidateString);
System.out.println(matchHelper[0] + startIndex);
//Find the starting point of the second 'Bond'
matcher.find();
int nextIndex = matcher.start();
System.out.println(candidateString);
System.out.println(matchHelper[1] + nextIndex);
执行输出:
My name is Kuang. Kuang ZW.
^11
My name is Kuang. Kuang ZW.
^18
4.5 public int start(int group)
指定你感兴趣的sub group,然后返回sup group匹配的开始位置。
4.6 public int end()
这个和start()对应,返回在以前的匹配操作期间,由给定组所捕获子序列的最后字符之后的偏移量。
其实start和end经常是一起配合使用来返回匹配的子字符串。
4.7 public int end(int group)
和public int start(int group)对应,返回在sup group匹配的子字符串最后一个字符在整个字符串下标加一。
4.8 public String group()
返回由以前匹配操作所匹配的输入子序列。
这个方法提供了强大而方便的工具,它可以等同使用start和end,然后对字符串作substring(start, end)操作。
4.9 public String group(int group)
提供了强大而方便的工具,可以得到指定的group所匹配的输入字符串,前面的匹配组有相关的例子。
4.10 public int groupCount()
这个方法返回了,正则表达式的匹配的组数。
4.11 public boolean matches()
尝试将整个区域与模式匹配,这个要求整个输入字符串都要和正则表达式匹配。
4.12 public boolean find()
find会在整个输入中寻找是否有匹配的子字符串。
4.13 public boolean find(int start)
从输入字符串指定的start位置开始查找。
4.14 public boolean lookingAt()
基本上是matches更松约束的一个方法,尝试将从区域开头开始的输入序列与该模式匹配。