使用模式和匹配器从字符串解码多项式

Decode polynomial from String with Pattern and Matcher

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

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

您的正则表达式是正确的。主要问题是在抓取组之前缺少 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