2

我想将输入中的所有数字添加到coollection并对其进行排序。

我有这样的输入文件:

12i -+3456i
78,i910 
11
i-12i


13.14r
15.r16r
i17.18

-+19.20
+r21.22
+23.242526r

+-27.28r
-29.30r
-.313233r
r-0.343536rr


r.34r
3536.r
r+37.38

Liczba -0.1234 jest mniejsza niz liczba .2 i wieksza niz liczba -1;

i123456789123456789123456789123456789123456789123456789123456789123456789123456789123456789i

当然还有整数、双精度、大整数等。我想将它们放入 Collection 和排序中。如果我通过此输入进行 3 次传递,这并不难:

1. Create regex for integers and filter the input add those integers into collection
2.  Create regex for doubles and filter the input add those doubles into collection
3.  Create regex for bigIntegers and filter the input add those bigIntegers into collection
4.Sort this collection of BigDecimals.

但这似乎很愚蠢。有没有办法通过输入一次将所有这些数字放入集合中?使用正则表达式。

编辑:

12,12 == 12.12 --> double
12i --> i does not count this is integer 12

EDIT2:正确的输出顺序

-29.30
-27.28
-12
-1
-0.343536
-0.313233
-0.1234
0.2
0.34
11
12
13.14
15
16
17.18
19.20
21.22
23.242526
37.38
78
910
3456
3536
123456789123456789123456789123456789123456789123456789123456789123456789123456789123456789
4

3 回答 3

4
List<String> matchList = new ArrayList<String>();
Pattern regex = Pattern.compile("-?(?:\\d+(?:\\.\\d+)?|\\.\\d+)");
Matcher regexMatcher = regex.matcher(subjectString);
while (regexMatcher.find()) {
    matchList.add(regexMatcher.group());
} 

为您提供ArrayList所有正则表达式匹配项。如果我了解 Java,我可能会向您展示如何将其更改ArrayList为 BigDecimals,但我想您可以自己做。然后就整理好了。

解释:

-?          # Match an optional minus sign.
(?:         # Either match:
 \d+        # a number,
 (?:\.\d+)? # optionally followed by a decimal part
|           # or
 \.\d+      # just a decimal part
)           # End of alternation

regex101中查看它的实际效果。

(编辑:不会产生大量空匹配的新版本(旧版本也匹配空字符串)

于 2013-01-15T17:54:26.080 回答
0

您可以为所有数字制作一个正则表达式,例如"[+-]?\\d*[,.]?\\d*"并将它们解析为 BigDecimalsnew BigDecimal(matcher.group().replace(",", ".")

于 2013-01-15T17:51:13.057 回答
0

我会做以下事情

public static List<Number> parse(Reader reader) throws IOException {
    List<BigDecimal> numbers = new ArrayList<>();
    StringBuilder num = new StringBuilder();
    for (int ch; (ch = reader.read()) >= 0; ) {
        if ((ch >= '0' && ch <= '9') || ch == '.' || ch == '-') {
            num.append((char) ch);
        } else {
            removeLast(num, '.');
            removeLast(num, '-');
            if (num.length() > 0)
                numbers.add(new BigDecimal(num.toString()));
            num.setLength(0);
        }
    }
    Collections.sort(numbers);
    List<Number> ret = new ArrayList<>();
    for (BigDecimal bd : numbers) {
        if (bd.compareTo(BigDecimal.valueOf(bd.intValue())) == 0) {
            ret.add(bd.intValue());
        } else if (bd.setScale(0, RoundingMode.DOWN).compareTo(bd) == 0) {
            ret.add(bd.setScale(0, RoundingMode.DOWN).toBigInteger());
        } else {
            ret.add(bd.doubleValue());
        }
    }
    return ret;
}

private static void removeLast(StringBuilder num, char ch) {
    if (num.length() > 0 && num.charAt(num.length() - 1) == ch)
        num.setLength(num.length() - 1);
}

public static void main(String... args) throws IOException, InterruptedException {
    String text = "12i -+3456i\n" +
            "78,i910 \n" +
            "11\n" +
            "i-12i\n" +
            "\n" +
            "\n" +
            "13.14r\n" +
            "15.r16r\n" +
            "i17.18\n" +
            "\n" +
            "-+19.20\n" +
            "+r21.22\n" +
            "+23.242526r\n" +
            "\n" +
            "+-27.28r\n" +
            "-29.30r\n" +
            "-.313233r\n" +
            "r-0.343536rr\n" +
            "\n" +
            "\n" +
            "r.34r\n" +
            "3536.r\n" +
            "r+37.38\n" +
            "\n" +
            "Liczba -0.1234 jest mniejsza niz liczba .2 i wieksza niz liczba -1;\n" +
            "\n" +
            "i123456789123456789123456789123456789123456789123456789123456789123456789123456789123456789i\n";
    for (Number number : parse(new StringReader(text))) {
        System.out.println(number);
    }
}

印刷

-29.3
-27.28
-12
-1
-0.343536
-0.313233
-0.1234
0.2
0.34
11
12
13.14
15
16
17.18
19.2
21.22
23.242526
37.38
78
910
3456
3536
123456789123456789123456789123456789123456789123456789123456789123456789123456789123456789

注意:int、double 或 BigInteger 都不会打印为-29.30or19.20

于 2013-01-15T18:00:25.453 回答