如何在给定数字中缺少数字的字段中用数字拆分?

How to splite with number with , in filed with missing number in given number?

我正在尝试获取此输出。但是未能通过这个 ',' 分隔丢失数字 ?

Input: 1, 2, 3, 4.. 9, 10,13.. 17, 18, 19.. 25 
Output: 1 2 3 4 5 6 7 8 9 10 13 14 15 16 17 18 19 20 21 22 23 24 25

这是一个可以满足您需要的工作实现,代码下方有进一步的解释:

public static String getCSV(int start, int end) {
    List<String> list = IntStream.range(start, end).boxed()
       .map(i -> i.toString()).collect(Collectors.toList());
    String csv = String.join(" ", list);

    return csv;
}

public static void main(String args[])
{
    String input = "1, 2, 3, 4.. 9, 10,13.. 17, 18, 19.. 25";
    input = input.replaceAll(",\s*", " ");
    Pattern r = Pattern.compile("(\d+)\.\.\s*(\d+)");
    Matcher m = r.matcher(input);
    StringBuffer stringBuffer = new StringBuffer();
    while (m.find( )) {
        int start = Integer.parseInt(m.group(1));
        int end = Integer.parseInt(m.group(2));
        m.appendReplacement(stringBuffer, getCSV(start, end+1));
    }
    System.out.println(stringBuffer);
}

输出:

1 2 3 4 5 6 7 8 9 10 13 14 15 16 17 18 19 20 21 22 23 24 25

此方法使用正则表达式匹配器来识别省略号中的每一对数字(例如4.. 9),然后将其替换为从起点到终点的space分隔的连续范围. Java 8 个流在这里派上用场,getCSV() 方法生成一个字符串,其中包含来自起始和结束输入值的数字序列。然后,我们只需要遍历整个输入,并使用辅助方法替换每个省略号。

Demo