使用正则表达式确定是否为十进制,分数或无效

时间:2013-12-01 10:45:27

标签: java regex

如果用户输入十进制,分数,混合分数或无效,我如何确定用户的输入?

这是我程序中允许的输入

String[] s = new String[]{ "0",".",".0",".01","1.","1.0","1.01", "1 ","1 1","1/2","1/","1/0", "1 1/1", "1 0/0","1 /","1 /0", "1 0/"};

预期产出:

0) 0  ----  Zero

1) .  ----  Error

2) .0  ----  Zero

3) 0.01  ----   Decimal

4) 1.  ----  Decimal(the input 1. should be turned to decimal)

5) 1.0  ----   Decimal

6) 1.01  ----   Decimal

7) 1   ----  Decimal(the input with space after the whole number must be decimal)

8) 1 1  ----  Error(because of the space between the 1)

9) 1/2  ----  Fraction

10) 1/  ----  Error

11) 1/0  ----  Error

12) 1 1/1  ----  Mixed Fraction

13) 1 0/0  ----  Error

14) 1 /  ----  Error

15) 1 /0  ----  Error

16) 1 0/  ----  Error

实际输出:

0) 0  ----  Zero

1) .  ----  Error

2) .0  ----  Zero

3) 0.01  ----   Decimal

4) 1.  ----  Error

5) 1.0  ----   Decimal

6) 1.01  ----   Decimal

7) 1   ----  Error

8) 1 1  ----  Mixed Fraction

9) 1/2  ----  Fraction

10) 1/  ----  Fraction

11) 1/0  ----  Fraction

12) 1 1/1  ----  Mixed Fraction

13) 1 0/0  ----  Error

14) 1 /  ----  Error

15) 1 /0  ----  Error

16) 1 0/  ----  Error

我对如何建立良好的正则表达式模式知之甚少?

我的代码:

public class DecimalOrFraction {
    public static void main(String args[]){
        String[] s = new String[]{ "0",".",".0",".01","1.","1.0","1.01", "1 ","1 1","1/2","1/","1/0", "1 1/1", "1 0/0","1 /","1 /0", "1 0/"};

        for (int x = 0 ;x<s.length;x++) {
            if(s[x].matches("[0]?([.][0]{1,3}\\s{0,1})?")){
                System.out.println(x+") "+s[x]+"  ----  Zero"); 
            }
            else if(s[x].matches("\\d{0,3}([.]\\d{1,3}\\s{0,1})?")){
                System.out.println(x+") "+Float.valueOf(s[x])+"  ----   Decimal");
            }
            else if(s[x].matches("[1-9]{1,5}([.]\\d{1,3})?\\s[1-9]\\d{0,2}([/]\\d{0,3})?")){
                System.out.println(x+") "+s[x]+"  ----  Mixed Fraction");
            }
            else if(s[x].matches("[1-9]\\d{0,4}[/]\\d{0,3}")){
                System.out.println(x+") "+s[x]+"  ----  Fraction");
            } 
            else{
                System.out.println(x+") "+s[x]+"  ----  Error");    
            }
        }
    }

}

3 个答案:

答案 0 :(得分:0)

为了展示在多个部分拆分功能时你可以做些什么,我创建了一些代码。请注意,这不是一个明确的(类)设计。

这段代码的优点是你可以用你解析它们的值实际。提供良好的错误消息也很有用。如果您更加分割main方法,则可以创建一个实时验证值的解析器(例如,如果要验证GUI中的值,则非常有用)。

public class Numbers {

private static final String[] s = new String[]{ "0",".",".0",".01","1.","1.0","1.01", "1 ","1 1","1/2","1/","1/0", "1 1/1", "1 0/0","1 /","1 /0", "1 0/"};

    private static final Pattern FRACTION_PATTERN = Pattern.compile("(?:(\\d+)\\s+)?(\\d+)/(\\d+)");

    public static void main(String[] args) {
        int counter = 0;
        for (String value : s) {
            counter++;
            String trimmedValue = value.trim();
            if (isNumber(trimmedValue)) {
                System.out.printf("%d) %s ---- Natural number%n", counter, trimmedValue);
                continue;
            }

            boolean parseDecimal = isProbablyDecimal(trimmedValue);
            boolean parseFraction = isProbablyFraction(trimmedValue);

            if (parseDecimal && parseFraction) {
                System.out.printf("%d) %s ---- Error (both decimal & fraction)%n", counter, trimmedValue);
                continue;
            }

            if (parseDecimal) {
                if (!trimmedValue.matches("\\d*[.]\\d+")) {
                    System.out.printf("%d) %s ---- Error (invalid decimal)%n", counter, trimmedValue);
                    continue;
                }
                System.out.printf("%d) %s ---- Decimal value%n", counter, trimmedValue);
                continue;
            }

            if (parseFraction) {
                Matcher m = FRACTION_PATTERN.matcher(trimmedValue);
                if (!m.matches()) {
                    System.out.printf("%d) %s ---- Error (invalid faction)%n", counter, trimmedValue);
                    continue;
                }
                if (Integer.valueOf(m.group(3)) == 0) {
                    System.out.printf("%d) %s ---- Error (division by zero in fraction)%n", counter, trimmedValue);
                    continue;
                }
                if (m.group(1) != null) {
                    System.out.printf("%d) %s ---- Mixed fraction%n", counter, trimmedValue);
                    continue;
                }

                System.out.printf("%d) %s ---- Fraction%n", counter, trimmedValue);
                continue;
            }

            System.out.printf("%d) %s ---- Error (Cannot make bread out of it)%n", counter, trimmedValue);
            continue;

        }
    }

    public static boolean isNumber(String value) {
        return value.matches("\\d+");
    }

    public static boolean isProbablyDecimal(String value) {
        // note, some countries use comma instead of dot
        return value.contains(".");
    }

    public static boolean isProbablyFraction(String value) {
        return value.contains("/");
    }
}

输出:

1) 0 ---- Natural number
2) . ---- Error (invalid decimal)
3) .0 ---- Decimal value
4) .01 ---- Decimal value
5) 1. ---- Error (invalid decimal)
6) 1.0 ---- Decimal value
7) 1.01 ---- Decimal value
8) 1 ---- Natural number
9) 1 1 ---- Error (Cannot make bread out of it)
10) 1/2 ---- Fraction
11) 1/ ---- Error (invalid faction)
12) 1/0 ---- Error (division by zero in fraction)
13) 1 1/1 ---- Mixed fraction
14) 1 0/0 ---- Error (division by zero in fraction)
15) 1 / ---- Error (invalid faction)
16) 1 /0 ---- Error (invalid faction)
17) 1 0/ ---- Error (invalid faction)

答案 1 :(得分:0)

试试这个: \d+\.\d+|\d+\.$|^.0$|\d+/[1-9]+|\d+ \d+/[1-9]+

答案 2 :(得分:0)

这会返回示例的正确值:

^(\d*\.?\d*)\s?(\d+/[1-9]\d*)?$

Working example on RegExr

在Java中,它将是:^(\\d*\\.?\\d*)\\s?(\\d+/[1-9]\\d*)?$