“a*”: 以字符’a’开头的字符串
“*bc*”: 包含字符串’bc’的字符串
“A??”: 以字符’A’开头,且长度为3的字符串
“?A?”: 以字符’A’居其间,且长度为3的字符串
class MyString{
String str;
//其它方法或成员定义
//构造函数
public Boolean isLike(String regex){
……
}
}
new MyString(“abcd”).isLike(“a*”)--true
new MyString(“ABCD”).isLike(“*C*”)--true
new MyString(“ABCD”).isLike(“ABC?”)--true
public class MyString {
private String str;
MyString(String str){
this.str=str;
}
public boolean isLike(String regex){
String re=regex;
re=re.replaceAll("[*]", "[a-zA-Z0-9]*");
re=re.replaceAll("[?]", ".");
Pattern p=Pattern.compile(re);
Matcher m=p.matcher(str);
return m.matches();
}
}