我知道我可以自己寫一個,但我希望我可以重用一個現有的。是否有一個函數可以將一個Ant樣式的glob模式轉換爲正則表達式?
螞蟻式球體與正常文件球體幾乎相同,並且'**'與子目錄相匹配。
FWIW,我的實現是:
public class AntGlobConverter {
public static String convert(String globExpression) {
StringBuilder result = new StringBuilder();
for (int i = 0; i != globExpression.length(); ++i) {
final char c = globExpression.charAt(i);
if (c == '?') {
result.append('.');
} else if (c == '*') {
if (i + 1 != globExpression.length() && globExpression.charAt(i + 1) == '*') {
result.append(".*");
++i;
} else {
result.append("[^/]*");
}
} else {
result.append(c);
}
}
return result.toString();
}
}