我正在尝试实施Levenshtein距离算法以找到最相似的匹配
列表就像
List<String> values = new ArrayList<String>();
values.add("*");
values.add("3.63.161.2");
values.add("3*");
values.add("*2");
values.add("3*1");
values.add("3.1*");
values.add("3.2*");
values.add("3.3*");
values.add("3.4*");
values.add("3.5*");
values.add("3.61*");
values.add("3*0");
values.add("2*");
values.add("1*");
values.add("3.62*");
String stringToMatch = "3.63.162.2";
for (String pattern : values) {
String regex = pattern.replace(".", "\\.").replace("*", ".*");
System.out.println("String ["+stringToMatch+"] matches with ["+pattern+"] : "+ stringToMatch.matches(regex) +" and the difference is --> "+StringUtils.getLevenshteinDistance(stringToMatch, pattern));
}
这是我期待的输出
String [3.63.162.2] matches with [*] : true and the difference is --> 10
String [3.63.162.2] matches with [3.63.161.2] : false and the difference is --> 1
String [3.63.162.2] matches with [3*] : true and the difference is --> 9
String [3.63.162.2] matches with [*2] : true and the difference is --> 9
String [3.63.162.2] matches with [3*1] : false and the difference is --> 8
String [3.63.162.2] matches with [3.1*] : false and the difference is --> 7
String [3.63.162.2] matches with [3.2*] : false and the difference is --> 7
String [3.63.162.2] matches with [3.3*] : false and the difference is --> 7
String [3.63.162.2] matches with [3.4*] : false and the difference is --> 8
String [3.63.162.2] matches with [3.5*] : false and the difference is --> 8
String [3.63.162.2] matches with [3.61*] : false and the difference is --> 6
String [3.63.162.2] matches with [3*0] : false and the difference is --> 9
String [3.63.162.2] matches with [2*] : false and the difference is --> 9
String [3.63.162.2] matches with [1*] : false and the difference is --> 9
String [3.63.162.2] matches with [3.62*] : false and the difference is --> 6
所以最近的匹配字符串是
String [3.63.162.2] matches with [3*] : true and the difference is --> 9
String [3.63.162.2] matches with [*2] : true and the difference is --> 9
我想要的是给3 *作为最匹配,因为第一个字符匹配。
有没有办法使用相同的技术从左到右匹配字符串。
我知道我可以单独编写匹配的代码,但我也想看看其他方法。