正则表达式中Matcher上的4个方法
boolean matches()
boolean lookingAt()
boolean find()
boolean find(int start)
matches()
方法用来判断整个输入字符串是否匹配正则表达式模式lookingAt()
用来判断该字符串(不必是整个字符串)的起始部分是否能够匹配模式find()
用来在CharSequence
中查找多个匹配。
//: strings/Finding.java
import java.util.regex.*;
import static net.mindview.util.Print.*;
public class Finding {
public static void main(String[] args) {
Matcher m = Pattern.compile("\\w+")
.matcher("Evening is full of the linnet's wings");
while(m.find())
printnb(m.group() + " ");
print();
int i = 0;
while(m.find(i)) {
printnb(m.group() + " ");
i++;
}
}
} /* Output:
Evening is full of the linnet s wings
Evening vening ening ning ing ng g is is s full full ull ll l of of f the the he e linnet linnet innet nnet net et t s s wings wings ings ngs gs s
*///:~
find()
像迭代器那样遍历输入的字符串,而第二个带参的find(int start)
则是接受一个参数,该参数表示字符串中字符的位置,并以其为搜索的起点。