/**
* 也许还存在不能完全处理的情形,但是,对于常规的java类名和包名等通配符匹配,则完全可以胜任!
* @author wuhe.jyh
*
*/
public class WildMatch {
private static boolean wildMatch(String pattern, String str) {
pattern = toJavaPattern(pattern);
System.out.println( "pattern: " + pattern);
return java.util.regex.Pattern.matches(pattern, str);
}
private static String toJavaPattern(String pattern) {
String result = "^";
char metachar[] = { '$', '^', '[', ']', '(', ')', '{', '|', '+', '.',
'/', '\\' };
for ( int i = 0; i < pattern.length(); i++) {
char ch = pattern.charAt(i);
boolean isMeta = false;
for ( int j = 0; j < metachar.length; j++) {
if (ch == metachar[j]) {
result += "\\" + ch;
isMeta = true;
break;
}
}
// 对 * 和 ? 进行特殊处理
if (!isMeta) {
if (ch == '*') {
result += ".*";
} else if (ch == '?') {
result += '.';
} else {
result += ch;
}
}
}
result += "$";
return result;
}
public static void main(String[] args) {
test( "*", "toto");
test( "toto.java", "tutu.java");
test( "12345", "1234");
test( "1234", "12345");
test( "*f", "");
test( "***", "toto");
test( "*.java", "toto.");
test( "*.java", "toto.jav");
test( "*.java", "toto.java");
test( "abc*", "");
test( "a*c", "abbbbbccccc");
test( "abc*xyz", "abcxxxyz");
test( "*xyz", "abcxxxyz");
test( "abc**xyz", "abcxxxyz");
test( "abc**x", "abcxxx");
test( "*a*b*c**x", "aaabcxxx");
test( "abc*x*yz", "abcxxxyz");
test( "abc*x*yz*", "abcxxxyz");
test( "a*b*c*x*yf*z*", "aabbccxxxeeyffz");
test( "a*b*c*x*yf*zze", "aabbccxxxeeyffz");
test( "a*b*c*x*yf*ze", "aabbccxxxeeyffz");
test( "a*b*c*x*yf*ze", "aabbccxxxeeyfze");
test( "*LogServerInterface*.java", "_LogServerInterfaceImpl.java");
test( "abc*xyz", "abcxyxyz");
test( "a?", "abc");
test( "a?", "a3");
test( "a?", "a$");
test( "?a", "?a");
}
private static void test(String pattern, String str) {
System.out.println(pattern + ", " + str + " =>> "
+ wildMatch(pattern, str));
}
}
* 也许还存在不能完全处理的情形,但是,对于常规的java类名和包名等通配符匹配,则完全可以胜任!
* @author wuhe.jyh
*
*/
public class WildMatch {
private static boolean wildMatch(String pattern, String str) {
pattern = toJavaPattern(pattern);
System.out.println( "pattern: " + pattern);
return java.util.regex.Pattern.matches(pattern, str);
}
private static String toJavaPattern(String pattern) {
String result = "^";
char metachar[] = { '$', '^', '[', ']', '(', ')', '{', '|', '+', '.',
'/', '\\' };
for ( int i = 0; i < pattern.length(); i++) {
char ch = pattern.charAt(i);
boolean isMeta = false;
for ( int j = 0; j < metachar.length; j++) {
if (ch == metachar[j]) {
result += "\\" + ch;
isMeta = true;
break;
}
}
// 对 * 和 ? 进行特殊处理
if (!isMeta) {
if (ch == '*') {
result += ".*";
} else if (ch == '?') {
result += '.';
} else {
result += ch;
}
}
}
result += "$";
return result;
}
public static void main(String[] args) {
test( "*", "toto");
test( "toto.java", "tutu.java");
test( "12345", "1234");
test( "1234", "12345");
test( "*f", "");
test( "***", "toto");
test( "*.java", "toto.");
test( "*.java", "toto.jav");
test( "*.java", "toto.java");
test( "abc*", "");
test( "a*c", "abbbbbccccc");
test( "abc*xyz", "abcxxxyz");
test( "*xyz", "abcxxxyz");
test( "abc**xyz", "abcxxxyz");
test( "abc**x", "abcxxx");
test( "*a*b*c**x", "aaabcxxx");
test( "abc*x*yz", "abcxxxyz");
test( "abc*x*yz*", "abcxxxyz");
test( "a*b*c*x*yf*z*", "aabbccxxxeeyffz");
test( "a*b*c*x*yf*zze", "aabbccxxxeeyffz");
test( "a*b*c*x*yf*ze", "aabbccxxxeeyffz");
test( "a*b*c*x*yf*ze", "aabbccxxxeeyfze");
test( "*LogServerInterface*.java", "_LogServerInterfaceImpl.java");
test( "abc*xyz", "abcxyxyz");
test( "a?", "abc");
test( "a?", "a3");
test( "a?", "a$");
test( "?a", "?a");
}
private static void test(String pattern, String str) {
System.out.println(pattern + ", " + str + " =>> "
+ wildMatch(pattern, str));
}
}
转载于:https://blog.51cto.com/memory/1170194