String regex = "\\w\\s-\\s\\d+";
String input = "a - 11 h - 19 l - 18 d - 19";
Pattern pattern = Pattern.compile(regex);
Matcher matcher = pattern.matcher(input);
matcher.toMatchResult().groupCount(); // this should return 4 (won't be used in final code)
将每个子字符串收集到列表中
List<String> strings = new ArrayList<>();
while (matcher.find()) {
strings.add(matcher.group());
}
StringBuilder buffer = new StringBuilder();
newList.forEach(item -> {
buffer.append(item + " "); // done to reinsert the space that separated each substring.
});
我创建了一个测试程序来运行它:
public class SortBySubstringDemo {
public static void main(String[] args) {
String regex = "\\w\\s-\\s\\d+";
String input = "a - 11 h - 19 l - 18 d - 19";
System.out.println("Input:\n" + input);
Pattern pattern = Pattern.compile(regex);
Matcher matcher = pattern.matcher(input);
List<String> strings = new ArrayList<>();
while (matcher.find()) {
strings.add(matcher.group());
}
Comparator<String> compareBySubstring = Comparator.comparing((String s) -> s.substring(s.indexOf(" -")))
.reversed().thenComparing((String s) -> s.substring(0, s.indexOf("-")));
List<String> newList = strings.stream().sorted(compareBySubstring).collect(Collectors.toList());
StringBuilder buffer = new StringBuilder();
newList.forEach(item -> {
buffer.append(item + " ");
});
String output = buffer.toString().trim();
System.out.println("Output:\n" + output);
}
}
其结果如下:
Input:
a - 11 h - 19 l - 18 d - 19
Output:
d - 19 h - 19 l - 18 a - 11
To solve, the problem must be broken down into subproblems:
Break down the input string into substrings
Collect each substring into a list
Create a comparator that compares the last (numeric) portion of the (sub)string and sorts them in descending order, and then by the beginning portion in ascending order
Convert the list of substrings back into a string
Break down the input string into substrings
String regex = "\\w\\s-\\s\\d+";
String input = "a - 11 h - 19 l - 18 d - 19";
Pattern pattern = Pattern.compile(regex);
Matcher matcher = pattern.matcher(input);
matcher.toMatchResult().groupCount(); // this should return 4 (won't be used in final code)
Collect each substring into a list
List<String> strings = new ArrayList<>();
while (matcher.find()) {
strings.add(matcher.group());
}
The code above will iterate through all of the groups that matched the regex pattern and adds them to a list.
The comparator I created compares the last portion of the substrings (after the dash) and sorts them in descending order (reversed()). Then, the intermediate result is sorted in ascending order from the beginning of the substring up to the dash. Basically it sorts the intermediate result in alphabetical order since the substrings start with a letter.
Convert the list of substrings back into a string
StringBuilder buffer = new StringBuilder();
newList.forEach(item -> {
buffer.append(item + " "); // done to reinsert the space that separated each substring.
});
I created a test program to run this:
public class SortBySubstringDemo {
public static void main(String[] args) {
String regex = "\\w\\s-\\s\\d+";
String input = "a - 11 h - 19 l - 18 d - 19";
System.out.println("Input:\n" + input);
Pattern pattern = Pattern.compile(regex);
Matcher matcher = pattern.matcher(input);
List<String> strings = new ArrayList<>();
while (matcher.find()) {
strings.add(matcher.group());
}
Comparator<String> compareBySubstring = Comparator.comparing((String s) -> s.substring(s.indexOf(" -")))
.reversed().thenComparing((String s) -> s.substring(0, s.indexOf("-")));
List<String> newList = strings.stream().sorted(compareBySubstring).collect(Collectors.toList());
StringBuilder buffer = new StringBuilder();
newList.forEach(item -> {
buffer.append(item + " ");
});
String output = buffer.toString().trim();
System.out.println("Output:\n" + output);
}
}
Which results in the following:
Input:
a - 11 h - 19 l - 18 d - 19
Output:
d - 19 h - 19 l - 18 a - 11
发布评论
评论(3)
为了解决这个问题,必须将问题分解为子问题:
将输入字符串分解为子字符串
将每个子字符串收集到列表中
上面的代码将迭代与正则表达式模式匹配的所有组并将它们添加到列表中。
创建比较器
我创建的比较器比较子字符串的最后部分(破折号之后)并按降序对它们进行排序 (
reversed()
)。然后,中间结果从子字符串的开头到破折号按升序排序。基本上,它按字母顺序对中间结果进行排序,因为子字符串以字母开头。将子字符串列表转换回字符串
我创建了一个测试程序来运行它:
其结果如下:
To solve, the problem must be broken down into subproblems:
Break down the input string into substrings
Collect each substring into a list
The code above will iterate through all of the groups that matched the regex pattern and adds them to a list.
Create a comparator
The comparator I created compares the last portion of the substrings (after the dash) and sorts them in descending order (
reversed()
). Then, the intermediate result is sorted in ascending order from the beginning of the substring up to the dash. Basically it sorts the intermediate result in alphabetical order since the substrings start with a letter.Convert the list of substrings back into a string
I created a test program to run this:
Which results in the following:
针对您的具体示例的一种替代方法是使用
stream
:" - "
替换为" "
来获取由空格分隔的字母和数字的字符串。" "
分割得到一个数组
,其中偶数索引为字母,奇数索引为数字。IntStream
,并使用array[index]
作为键和将它们映射到
作为值。Map.Entry
>array[index + 1]流
进行排序,该比较器首先比较值,然后比较键。key + " - " + value
将条目stream
映射到字符串。Collectors.joining(" ")
将流
收集到一个字符串,并用空格分隔每个“字母 - 数字”
。将所有内容总结在一个方法中:
测试:
输出:
One alternative for your specific example is to use
stream
:" - "
with" "
to obtain a string with letters and numbers separated by whitespace." "
to obtain anarray
with letters in even indexes and numbers in odd indexes.IntStream
with even indexes and map them to aMap.Entry<String,Integer>
usingarray[index]
as key andarray[index + 1]
as value.stream
with a comparator that compares values first and then keys.stream
to a string withkey + " - " + value
.stream
to a string usingCollectors.joining(" ")
to separate each"letter - number"
with a whitespace.Summarizing all in a method:
Test:
Output: