public Matcher matcher(){
return new Matcher(this);
}
public Matcher matcher(String s){
Matcher m=new Matcher(this);
m.setTarget(s);
return m;
}
public Matcher matcher(char[] data,int start,int end){
Matcher m=new Matcher(this);
m.setTarget(data,start,end);
return m;
}
public Matcher matcher(MatchResult res,int groupId){
Matcher m=new Matcher(this);
if(res instanceof Matcher){
m.setTarget((Matcher)res,groupId);
}
else{
m.setTarget(res.targetChars(),res.start(groupId)+res.targetStart(),res.length(groupId));
}
return m;
}
public Matcher matcher(Reader text,int length)throws IOException{
Matcher m=new Matcher(this);
m.setTarget(text,length);
return m;
}
以上都是用来实现第一种返回matcher的方法。可以看到,这几种实现都是通过new Matcher实例, 以this(即Pattern实例)为参数,然后根据参数不同调用Matcher.setTarget()方法。
public Matcher matcher(MatchResult res,String groupName){
Integer id=res.pattern()。groupId(groupName);
if(id==null) throw new IllegalArgumentException("group not found:"+groupName);
int group=id.intValue();
return matcher(res,group);
}
这是第二种返回matcher的方法。
从这里我们可以发现一种比较好的方法:当你需要两个互相关联的类,一个类的实例需要构造另一 个类的实例,可以在第一个类中用一个public方法,方法实现为调用第二个类的构造函数,并可以 用构造出来的实例调用其他方法,根据第一个类的实例的数据来设置第二个类的实例。
同样的,也有Replacer, 和Tokenizer的构造方法。
public Replacer replacer(String eXPr){
return new Replacer(this,expr);
}
/**
* Returns a replacer will substitute all occurences of a pattern
* through applying a user-defined substitution model.
* @param model a Substitution object which is in charge for match substitution
* @see Replacer
*/
public Replacer replacer(Substitution model){
return new Replacer(this,model);
}
public RETokenizer tokenizer(String text){
return new RETokenizer(this,text);
}
/**
* Tokenizes a specified region by an occurences of the pattern.
* Note that a series of adjacent matches are regarded as a single separator.
* The same as new RETokenizer(Pattern,char[],int,int);
* @see RETokenizer
* @see RETokenizer#RETokenizer(jregex.Pattern,char[],int,int)
*/
public RETokenizer tokenizer(char[] data,int off,int len){
return new RETokenizer(this,data,off,len);
}
/**
* Tokenizes a specified region by an occurences of the pattern.
* Note that a series of adjacent matches are regarded as a single separator.
* The same as new RETokenizer(Pattern,Reader,int);
* @see RETokenizer
* @see RETokenizer#RETokenizer(jregex.Pattern,Java.io.Reader,int)
*/
public RETokenizer tokenizer(Reader in,int length) throws IOException{
return new RETokenizer(this,in,length);
}
回忆一下,我在本文的开头,曾经提到过:"一个好的库必须是一个紧凑的关系紧密的整体,而不 是一个分散的关系松散的对象的集合。