I am trying to implement the Levenshtein distance algorithm to find the most similar match
The list is like
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));
}
The output of this is what I am expecting
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
So the nearest matching strings are
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
What I would like to have is give 3* as the most matching because the first character is matching.
So is there a way to match the strings from left to right using the same technique.
I know I can separately write a code to match but I would like to look at other approaches as well.