mirror of
https://gitee.com/dromara/sa-token.git
synced 2025-10-21 19:17:25 +08:00
升级模糊匹配算法
This commit is contained in:
@@ -251,8 +251,39 @@ public class SaFoxUtil {
|
||||
if( ! patt.contains("*")) {
|
||||
return patt.equals(str);
|
||||
}
|
||||
// 正则匹配
|
||||
return Pattern.matches(patt.replace(".*", "\\..*"), str);
|
||||
// 深入匹配
|
||||
return vagueMatchMethod(patt, str);
|
||||
}
|
||||
|
||||
/**
|
||||
* 字符串模糊匹配
|
||||
*
|
||||
* @param pattern /
|
||||
* @param str /
|
||||
* @return /
|
||||
*/
|
||||
private static boolean vagueMatchMethod( String pattern, String str) {
|
||||
int m = str.length();
|
||||
int n = pattern.length();
|
||||
boolean[][] dp = new boolean[m + 1][n + 1];
|
||||
dp[0][0] = true;
|
||||
for (int i = 1; i <= n; ++i) {
|
||||
if (pattern.charAt(i - 1) == '*') {
|
||||
dp[0][i] = true;
|
||||
} else {
|
||||
break;
|
||||
}
|
||||
}
|
||||
for (int i = 1; i <= m; ++i) {
|
||||
for (int j = 1; j <= n; ++j) {
|
||||
if (pattern.charAt(j - 1) == '*') {
|
||||
dp[i][j] = dp[i][j - 1] || dp[i - 1][j];
|
||||
} else if (str.charAt(i - 1) == pattern.charAt(j - 1)) {
|
||||
dp[i][j] = dp[i - 1][j - 1];
|
||||
}
|
||||
}
|
||||
}
|
||||
return dp[m][n];
|
||||
}
|
||||
|
||||
/**
|
||||
|
Reference in New Issue
Block a user