正则表达式匹配十进制格式的数字,并检查小数点前后的数字计数

时间:2019-02-26 10:44:22

标签: java regex validation

我的十进制格式可能如下:

"#.#"
"#.##"
"#.00"
"##.###"
"##.000"
"###.###"
"#####.####"
"####.#####"
etc.

我想验证带有1个小数点的十进制格式数字[0-9]。

此外,我想包括以下检查:

1)小数点前,应等于或小于0或等于所计数的哈希标签。

2)小数点后,应等于或小于等于计数的哈希标记。

例如:

格式定义为"##.###",对于这种格式,有效格式号是在这种情况下小数点前为0或小于等于2的数字,1点和小于等于0的格式。或等于3个数字。 有效数字为:

"1",
"12",
"12.3",
"12.34",
"12.345",

无效数字为:

"1.3456", -- because there are 4 digits after decimal point
"12.34567", -- because there are 5 digits after decimal point
"123.3", -- because there are 3 digits before decimal point
"123.3454", -- because there are 3 digits before decimal point and 4 digits after decimal point

更新:

测试

format: ###.###
    1. 12.34 -> result: matches; expected: matches
    2. 123.456 -> result: matches; expected: matches
    3. 123.4567 -> result: matches; expected: not matches
    4. 1234.567 -> result: not matches; expected: not matches
    5. 1234.5678 -> result: not matches; expected: not matches

情况3失败。

用于测试图案和数字的代码:

public static void main(String[] args) throws Exception
    {

        double[] initialValues = {
            12.34,
            123.456,
            123.4567,
            1234.567,
            1234.5678,

            0,
            1,
            12,
            123,

            0.1,
            0.123,
            123.0,

            1.2,
            1.23,
            1.234,

            1.2,
            12.3,
            123.4,

            123456.678,
            12345.6789,

            1.23456789
        };

        String[] decimalFormats = {
            "###.###",

            "#.#",
            "#.##",
            "#.###",

            "#.#",
            "##.#",
            "###.#",

            "###.###",
            "#####.#####"
        };
        new Main().testParsingNumber(initialValues, decimalFormats);
    }

    public void testParsingNumber(double[] initialValues, String[] decimalFormats) throws ParseException {
        for (String decimalFormat : decimalFormats) {
            System.out.println(decimalFormat);

            for (double initialValue : initialValues) {
                String formattedDecimalNumber = new DecimalFormat(decimalFormat).format(initialValue);
                System.out.println("\t" + initialValue + " -> " + (isValidDecimalNumber(formattedDecimalNumber, decimalFormat) ? "matches" : "not matches"));
            }

            System.out.println("\n");
        }
    }

    public boolean isValidDecimalNumber(String formattedDecimalNumber, String decimalFormat) {
        List<String> digits = Arrays.asList(decimalFormat.split("[.]"));
        int maxNumbersBeforeDecimal = digits.get(0).length();
        int maxNumbersAfterDecimal = digits.get(1).length();

        //[0-9]{1,"+x1+"} => match digit before the decimal point with occurrence 1-x1
        //[0-9]{0,"+x2+"} => match digit after the decimal point 0-x2
        //.{0,1} => match the decimal point

        Pattern regexPattern = Pattern.compile("^[0-9]{1,"+ maxNumbersBeforeDecimal +"}.{0,1}[0-9]{0,"+ maxNumbersAfterDecimal +"}$");
        Matcher matcher = regexPattern.matcher(doubleValue);
        return matcher.matches();
    }

结果:

###.###
    12.34 -> matches
    123.456 -> matches
    123.4567 -> matches -- This should return false. Not correct! Returns true.
    1234.567 -> not matches -- This should return false. Correct!
    1234.5678 -> not matches -- This should return false. Correct!
    0.0 -> matches
    1.0 -> matches
    12.0 -> matches
    123.0 -> matches
    0.1 -> matches
    0.123 -> matches
    123.0 -> matches
    1.2 -> matches
    1.23 -> matches
    1.234 -> matches
    1.2 -> matches
    12.3 -> matches
    123.4 -> matches
    123456.678 -> not matches 
    12345.6789 -> not matches
    1.23456789 -> matches  -- Not correct


#.#
    12.34 -> not matches
    123.456 -> not matches
    123.4567 -> not matches
    1234.567 -> not matches
    1234.5678 -> not matches
    0.0 -> matches
    1.0 -> matches
    12.0 -> matches  -- Not correct
    123.0 -> matches  -- Not correct
    0.1 -> matches
    0.123 -> matches  -- Not correct
    123.0 -> matches  -- Not correct
    1.2 -> matches 
    1.23 -> matches  -- Not correct
    1.234 -> matches  -- Not correct
    1.2 -> matches
    12.3 -> not matches 
    123.4 -> not matches
    123456.678 -> not matches
    12345.6789 -> not matches
    1.23456789 -> matches  -- Not correct


#.##
    12.34 -> not matches
    123.456 -> not matches
    123.4567 -> not matches
    1234.567 -> not matches
    1234.5678 -> not matches
    0.0 -> matches
    1.0 -> matches
    12.0 -> matches
    123.0 -> matches
    0.1 -> matches
    0.123 -> matches
    123.0 -> matches
    1.2 -> matches
    1.23 -> matches
    1.234 -> matches
    1.2 -> matches
    12.3 -> not matches
    123.4 -> not matches
    123456.678 -> not matches
    12345.6789 -> not matches
    1.23456789 -> matches


#.###
    12.34 -> not matches
    123.456 -> not matches
    123.4567 -> not matches
    1234.567 -> not matches
    1234.5678 -> not matches
    0.0 -> matches
    1.0 -> matches
    12.0 -> matches
    123.0 -> matches
    0.1 -> matches
    0.123 -> matches
    123.0 -> matches
    1.2 -> matches
    1.23 -> matches
    1.234 -> matches
    1.2 -> matches
    12.3 -> not matches
    123.4 -> not matches
    123456.678 -> not matches
    12345.6789 -> not matches
    1.23456789 -> matches


#.#
    12.34 -> not matches
    123.456 -> not matches
    123.4567 -> not matches
    1234.567 -> not matches
    1234.5678 -> not matches
    0.0 -> matches
    1.0 -> matches
    12.0 -> matches
    123.0 -> matches
    0.1 -> matches
    0.123 -> matches
    123.0 -> matches
    1.2 -> matches
    1.23 -> matches
    1.234 -> matches
    1.2 -> matches
    12.3 -> not matches
    123.4 -> not matches
    123456.678 -> not matches
    12345.6789 -> not matches
    1.23456789 -> matches


##.#
    12.34 -> matches
    123.456 -> not matches
    123.4567 -> not matches
    1234.567 -> not matches
    1234.5678 -> not matches
    0.0 -> matches
    1.0 -> matches
    12.0 -> matches
    123.0 -> matches
    0.1 -> matches
    0.123 -> matches
    123.0 -> matches
    1.2 -> matches
    1.23 -> matches
    1.234 -> matches
    1.2 -> matches
    12.3 -> matches
    123.4 -> not matches
    123456.678 -> not matches
    12345.6789 -> not matches
    1.23456789 -> matches


###.#
    12.34 -> matches
    123.456 -> matches
    123.4567 -> matches
    1234.567 -> not matches
    1234.5678 -> not matches
    0.0 -> matches
    1.0 -> matches
    12.0 -> matches
    123.0 -> matches
    0.1 -> matches
    0.123 -> matches
    123.0 -> matches
    1.2 -> matches
    1.23 -> matches
    1.234 -> matches
    1.2 -> matches
    12.3 -> matches
    123.4 -> matches
    123456.678 -> not matches
    12345.6789 -> not matches
    1.23456789 -> matches


###.###
    12.34 -> matches
    123.456 -> matches
    123.4567 -> matches
    1234.567 -> not matches
    1234.5678 -> not matches
    0.0 -> matches
    1.0 -> matches
    12.0 -> matches
    123.0 -> matches
    0.1 -> matches
    0.123 -> matches
    123.0 -> matches
    1.2 -> matches
    1.23 -> matches
    1.234 -> matches
    1.2 -> matches
    12.3 -> matches
    123.4 -> matches
    123456.678 -> not matches
    12345.6789 -> not matches
    1.23456789 -> matches


#####.#####
    12.34 -> matches
    123.456 -> matches
    123.4567 -> matches
    1234.567 -> matches
    1234.5678 -> matches
    0.0 -> matches
    1.0 -> matches
    12.0 -> matches
    123.0 -> matches
    0.1 -> matches
    0.123 -> matches
    123.0 -> matches
    1.2 -> matches
    1.23 -> matches
    1.234 -> matches
    1.2 -> matches
    12.3 -> matches
    123.4 -> matches
    123456.678 -> not matches
    12345.6789 -> matches
    1.23456789 -> matches

1 个答案:

答案 0 :(得分:2)

这是我使用正则表达式的解决方案:

    String x = "###.###";
    List<String> s = Arrays.asList(x.split("[.]"));
    int x1 = s.get(0).length();
    int x2 = s.get(1).length();

    //[0-9]{1,"+x1+"} => match digit before the decimal point with occurrence 1-x1
    //[0-9]{0,"+x2+"} => match digit after the decimal point 0-x2
    //.{0,1} => match the decimal point
    Pattern p = Pattern.compile("^[0-9]{1,"+x1+"}.{0,1}[0-9]{0,"+x2+"}$");

    Matcher matcher = p.matcher("123.33");
    System.out.println(matcher.matches());//true