使用Pattern和Matcher从String中解码多项式

时间:2016-01-22 12:16:56

标签: java regex pattern-matching polynomials

我尝试解码表示多项式的字符串中的每个单项式。

public static void main(String[] args) {
    String polynomial = "3x^3-x^2+5.9x-3.8";
    String monomialFormat = "([+-]?[\\d\\.]*[a-zA-Z]?\\^?\\d*)", monomialPartsFormat = "([+-]?[\\d\\.]*)([a-zA-Z]?)\\^?(\\d*)";

    Pattern p1 = Pattern.compile(monomialFormat);
    Matcher m1 = p1.matcher(polynomial);

    while (!m1.hitEnd()) {
        m1.find();
        Pattern p2 = Pattern.compile(monomialPartsFormat);
        System.out.print(m1.group() + "   ->   ");
        Matcher m2 = p2.matcher(m1.group());

        float coefficient;
        try {
            coefficient = Float.valueOf(m2.group(1));
        } catch (IllegalStateException e) {
            coefficient = 0.0F;
        }
        int exponent;
        try {
            exponent = Integer.valueOf(m2.group(3));
        } catch (IllegalStateException e) {
            exponent = 0;
        }
        char variable;
        try {
            variable = m2.group(2).charAt(0);
        } catch (IllegalStateException e) {
            variable = '_';
        }

        System.out.println("" + coefficient + variable + "^" + exponent);
    }
}

第一个模式正常工作并将多项式切割成几个单项式,但不是第二个。没有找到每个单项式的不同部分(系数,变量,指数)的匹配。 第二个正则表达式肯定有问题,但我找不到它。

印刷品的结果:

3x^3   ->   0.0_^0
-x^2   ->   0.0_^0
+5.9x   ->   0.0_^0
-3.8   ->   0.0_^0

1 个答案:

答案 0 :(得分:2)

你的正则表达式是正确的。主要问题是在抓取组之前缺少 m2.find()。 在转换单项元素时,你也错过了一些额外的检查,所以我只是提供了一个小函数来帮助。

public static void main(String[] args) {

    String polynomial = "3x^3-x^2+5.9x-3.8";
    String monomialFormat = "([+-]?[\\d\\.]*[a-zA-Z]?\\^?\\d*)", monomialPartsFormat = "([+-]?[\\d\\.]*)([a-zA-Z]?)\\^?(\\d*)";

    Pattern p1 = Pattern.compile(monomialFormat);
    Matcher m1 = p1.matcher(polynomial);

    while (!m1.hitEnd()) {
        m1.find();
        Pattern p2 = Pattern.compile(monomialPartsFormat);
        System.out.print(m1.group() + "   ->   ");
        Matcher m2 = p2.matcher(m1.group());

        if (m2.find()) {     

            float coefficient;
            try {
                String coef = m2.group(1);
                if (isNumeric(coef)) {
                    coefficient = Float.valueOf(coef);
                } else {
                    coefficient = Float.valueOf(coef + "1");
                }
            } catch (IllegalStateException e) {
                coefficient = 0.0F;
            }

            int exponent;
            try {
                String exp = m2.group(3);
                if (isNumeric(exp)) {
                    exponent = Integer.valueOf(exp);
                } else {
                    exponent = 1;
                }
            } catch (IllegalStateException e) {
                exponent = 0;
            }

            String variable = m2.group(2);

            System.out.println("" + coefficient + variable + "^" + exponent);
        }
    }
}

public static boolean isNumeric(String str) {

    return str.matches("[+-]*\\d*\\.?\\d+");
}

输出:

3x^3   ->   3.0x^3
-x^2   ->   -1.0x^2
+5.9x   ->   5.9x^1
-3.8   ->   -3.8^1