Reputation: 326
I'm trying to implement a comparator for a TreeMap where the key entries are strings of the form
1a
2b
11a
11b
14
16
I can identify the strings that need extra processing easy enough using this regex
[0-9]+[a-zA-Z]+
using a simple [0-9]+ regex I can find the initial number on the string easily, my question is how can i split these up to let me then compare the integer values and the string characters separately?
Edit: Sample data is above the expected output ideally would be a string array with position 0 being the integer value and position 1 being the String value i.e.
[1,a]
[2,b]
[11,a]
[11,b]
Upvotes: 1
Views: 78
Reputation: 421310
Here's one approach that uses the regular expression that you suggest:
new Comparator<String>() {
Pattern p = Pattern.compile("([0-9]+)[a-zA-Z]+");
private String getNum(String s) {
Matcher m = p.matcher(s);
return m.matches() ? m.group(1) : s;
}
@Override
public int compare(String o1, String o2) {
o1 = getNum(o1);
o2 = getNum(o2);
return Integer.compare(Integer.parseInt(o1),
Integer.parseInt(o2));
}
};
If you're on Java 8, you could do
private static Pattern p = Pattern.compile("([0-9]+)[a-zA-Z]+");
private static int getNum(String s) {
Matcher m = p.matcher(s);
return Integer.parseInt(m.matches() ? m.group(1) : s);
}
and then use
Comparator.comparing(YourClass::getNum))
Another approach that doesn't use the regexp that you propose is to do
Comparator.comparing(s -> Integer.parseInt(s.replaceAll("[a-zA-Z]", ""))));
Upvotes: 2