Pattern和Matcher
一般来说,比起功能有限的String,我们更愿意构造功能强大的正则表达式对象。只需要导入java.util.regex包,然后用static Pattern.compile()方法来编译你的正则表达式即可。它会根据你的String类型的正则表达式生成一个pattern对象。接下来,把你想要检索的字符串传入Pattern对象的matcher()方法。mathcer()放法会生成一个Matcher对象,他有很多功能可用。列入,他的repalaceAll()方法能就爱那个所有匹配的部分都替换成你输入的参数。 作为一个示例,下面的类可以用来测试正则表达式,看看它们能否匹配一个输入的字符串。第一个控制台擦书是将用来搜索匹配输入的字符串。在Unix/Linux上,命令行中的正则表达式必须是引号括起。这个程序在测试正则表达式时很有用,特别是当你想验证它们是否具有你所期待的匹配功能的时候
//:strings/TestRegulaarExpression.java
//Allows you to easily try out regular expressions.
//{Args:abcabcabcdefabs "abc+" "(abc)+" "(abc){2,}"}
import java.util.regex.*;
public class TestRegularExpression{
public static void main(String[] args){
if(args.length<2){
System.out.println("Usage:\njava TestregularExpression "+"characterSequence regularExpression+");
}
System.out.println("Input:\""+args[0]+"\"");
for(String arg:args){
System.out.println("RegularExpression:\""+arg+"\"");
Pattern p=Pattern.compile(arg);
Matcher m=p.matcher(args[0]);
while(m.find()){
System.out.println("Match \""+m.group()+"\" at position "+m.start()+"-"+(m.end()-1));
}
}
}
}
运行结果为: D:>java TestRegularExpression abcabcabcdefabc "abc"+ "(abc)+" "(abc){2,}" Input:"abcabcabcdefabc" RegularExpression:"abcabcabcdefabc" Match "abcabcabcdefabc" at position 0-14 RegularExpression:"abc+" Match "abc" at position 0-2 Match "abc" at position 3-5 Match "abc" at position 6-8 Match "abc" at position 12-14 RegularExpression:"(abc)+" Match "abcabcabc" at position 0-8 Match "abc" at position 12-14 RegularExpression:"(abc){2,}" Match "abcabcabc" at position 0-8
Pattern对象表示编译后的正则表达式。测这个例子我们可以看出,我们使用已编译的Pattern上的matcher()方法,加上一个输入字符串,从额共同创造了一个matcher对象,同时,Pattern类还提供了static方法:
static boolean matches(String regex,CharSequence input) 该方法用于检查regex是否匹配整个CharSequence类型的input参数。年以后的pattern对象那个还提供了split()方法,他从匹配了regex的地方分割输入的字符串,返回分割后的子字符串String数组。 通过调用Pattrern.mathder()方法,并传入一个字符串参数,我们得到一个Matcher对象。使用Matcher上的方法,我们能够判断各种不同类型的匹配是否成功: boolean matches(); boolean lookingAt(); boolean find(); boolean find(int start); 其中的matches()方法用来判断整个输入字符串是否匹配正确的正则表达式,而lookingAt()则用来判断该字符串(不必是整个字符串)的开始部分是否能够匹配模式。 当我输入另外一个句子和表达式的时候 出现如下的结果,是在是想不明白:
D:>java TestRegularExpression Java now has regular expression ^Java \Breg.* s? s* s+ s{4} s{1}. s{0,3} Input:"Java" RegularExpression:"Java" Match "Java" at position 0-3 RegularExpression:"now" RegularExpression:"has" RegularExpression:"regular" RegularExpression:"expression" RegularExpression:"Java" Match "Java" at position 0-3 RegularExpression:"\Breg.*" RegularExpression:"s?" Match "" at position 0--1 Match "" at position 1-0 Match "" at position 2-1 Match "" at position 3-2 Match "" at position 4-3 RegularExpression:"software" RegularExpression:"Song Analysis" RegularExpression:"System Volume Information" RegularExpression:"s+" RegularExpression:"s{4}" RegularExpression:"s{1}." RegularExpression:"s{0,3}" Match "" at position 0--1 Match "" at position 1-0 Match "" at position 2-1 Match "" at position 3-2 Match "" at position 4-3
其中这些东西是怎么出现的啊? RegularExpression:"software" RegularExpression:"Song Analysis" RegularExpression:"System Volume Information"