小技巧共享(正则表达式)
我在平时积累的一些封装小工具,方便使用的,共享出来
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
* 正则表达式的封装,简化使用,原先使用多个类返回使用多步测试,比较麻烦
*/
package wwwlgy.tools.reg;
import java.util.regex.*;
/**
*
* @author l33187
*/
public class MyRegexp {
Pattern p;
Matcher m;
public MyRegexp (String rxp){
this.p = Pattern.compile(rxp);
}
/**
* 直接测试匹配,不用多步骤,一步到位
* @param str 要测试的字符串
* @return 返回匹配结果
*/
public boolean test(String str){
this.m = this.p.matcher(str);
return this.m.find();
}
////////////////////////////////////
//下面都是对Matcher的原始封装函数,不再说明
//////////////////////////////////////
public boolean matches(){
return this.m.matches();
}
public boolean find(){
return this.m.find();
}
public String group(){
return this.m.group();
}
public String group(int idx){
return this.m.group(idx);
}
}