使用正则表达式处理^符号以获取多项式的幂 [英] Processing ^ symbol for get powers of polynomials using regex

查看:65
本文介绍了使用正则表达式处理^符号以获取多项式的幂的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图处理一个字符串以检测多项式并获取其幂和系数,但是我的正则表达式似乎有问题,总是给我错误的结果,或者在某些情况下会导致异常.

 模式模式= Pattern.compile(([+-]?(?:(?:\\ d + x \\ ^ \\ d +)|(?:\\ d + x)|(?:\\ d +)|(?? x))));Matcher matcher = pattern.matcher(expr);while(matcher.find()){if(matcher.group(1).matches("[0-9] +")){一个= Integer.parseInt(matcher.group(1));b = 0;} else if(matcher.group(1).matches("\\ ^")){a = Integer.parseInt(expr.substring(0,expr.indexOf("x"))));b = Integer.parseInt(expr.substring(0,expr.indexOf("^"))+ 1);} 别的 {a = Integer.parseInt(expr.substring(0,expr.indexOf("x"))));b = 1;}} 

这应该能够检测并给我有效的输出,例如类似

5x ^ 3 + 2x +1

给出

作为输入.(这是更大方法的一部分,因此在被这些代码行处理之前,输入会被分成几部分)由于我是编程新手,所以我似乎无法理解问题是否出在正则表达式或Java语法上.

解决方案

我猜类似的表达式

  \ ^(-?\ d +(?:\.\ d +)?)|(-?\ d +(?:\.\ d +)?)|(x) 

示例2

  import java.util.regex.Matcher;导入java.util.regex.Pattern;公共类RegularExpression {公共静态void main(String [] args){final String regex ="\\ ^(-?\\ d +(?:\\.\\ d +)?)|(-?\\ d +(?:\\.\\ d +)?)|(x;final String string ="12x ^ 11 + 10x ^ 9 -8x ^ 7 + 9x ^ 6 -5x ^ 3 + 2x -1";最终模式模式= Pattern.compile(regex);最终Matcher Matcher = pattern.matcher(string);while(matcher.find()){System.out.println(完全匹配:" + matcher.group(0));for(int i = 1; i< = matcher.groupCount(); i ++){System.out.println("Group" + i +:" + matcher.group(i));}}}} 

输出

 完全匹配:12第1组:空第2组:12第3组:空全场比赛:x第1组:空第2组:空第3组:x全场比赛:^ 11小组1:11第2组:空第3组:空全场比赛:10第1组:空第2组:10第3组:空全场比赛:x第1组:空第2组:空第3组:x全场比赛:^ 9小组1:9第2组:空第3组:空全场比赛:-8第1组:空第2组:-8第3组:空全场比赛:x第1组:空第2组:空第3组:x全场比赛:^ 7小组1:7第2组:空第3组:空全场比赛:9第1组:空2组:9第3组:空全场比赛:x第1组:空第2组:空第3组:x全场比赛:^ 6第1组:6第2组:空第3组:空全场比赛:-5第1组:空第2组:-5第3组:空全场比赛:x第1组:空第2组:空第3组:x全场比赛:^ 3第1组:3第2组:空第3组:空全场比赛:2第1组:空第2组:2第3组:空全场比赛:x第1组:空第2组:空第3组:x全场比赛:-1第1组:空第2组:-1第3组:空 

I am trying to process a string to detect polynomials and get their powers and coefficients, but my regex seems to have a problem and always gives me wrong results or in some cases, causes an exception.

Pattern pattern = Pattern.compile("([+-]?(?:(?:\\d+x\\^\\d+)|(?:\\d+x)|(?:\\d+)|(?:x)))");
Matcher matcher = pattern.matcher(expr);
while (matcher.find()) {
    if(matcher.group(1).matches("[0-9]+")) {
        a = Integer.parseInt(matcher.group(1));
        b = 0;
    } else if(matcher.group(1).matches("\\^")) {
        a = Integer.parseInt(expr.substring(0, expr.indexOf("x")));
        b = Integer.parseInt(expr.substring(0, expr.indexOf("^"))+1);
    } else {
        a = Integer.parseInt(expr.substring(0, expr.indexOf("x")));
        b = 1;
    }
}

This should be able to detect and give me valid outputs if something like

5x^3 +2x +1

is given as an input. (This is part of a bigger method so input is broken into pieces before being processed by these lines of code) Since I am new to programming, I cannot seem to understand whether the problem is with regex or java syntax.

解决方案

I guess an expression similar to,

\^(-?\d+(?:\.\d+)?)|(-?\d+(?:\.\d+)?)|(x)

Demo (for decimals)

or,

\^(-?\d+)|(-?\d+)|(x)

Demo (for integers)

might be good to look into so that you'd solve the problem.


Here, the first group finds the exponent values, the second group returns the coefficients and the third group returns the x (so that you can figure out if there is a constant in the equation).

Test

import java.util.regex.Matcher;
import java.util.regex.Pattern;


public class RegularExpression{

    public static void main(String[] args){

        final String regex = "\\^(-?\\d+(?:\\.\\d+)?)|(-?\\d+(?:\\.\\d+)?)|(x)";
        final String string = "5x^3 +2x +1\n"
             + "5x^-3.1 +2x -1\n"
             + "5x^3.23 -2012.12x +10.12\n"
             + "-5x^3.54 +2x -1.512";

        final Pattern pattern = Pattern.compile(regex, Pattern.DOTALL | Pattern.CASE_INSENSITIVE);
        final Matcher matcher = pattern.matcher(string);

        while (matcher.find()) {
            System.out.println("Full match: " + matcher.group(0));
            for (int i = 1; i <= matcher.groupCount(); i++) {
                System.out.println("Group " + i + ": " + matcher.group(i));
            }
        }

    }
}

Output

Full match: 5
Group 1: null
Group 2: 5
Group 3: null
Full match: x
Group 1: null
Group 2: null
Group 3: x
Full match: ^3
Group 1: 3
Group 2: null
Group 3: null
Full match: 2
Group 1: null
Group 2: 2
Group 3: null
Full match: x
Group 1: null
Group 2: null
Group 3: x
Full match: 1
Group 1: null
Group 2: 1
Group 3: null
Full match: 5
Group 1: null
Group 2: 5
Group 3: null
Full match: x
Group 1: null
Group 2: null
Group 3: x
Full match: ^-3.1
Group 1: -3.1
Group 2: null
Group 3: null
Full match: 2
Group 1: null
Group 2: 2
Group 3: null
Full match: x
Group 1: null
Group 2: null
Group 3: x
Full match: -1
Group 1: null
Group 2: -1
Group 3: null
Full match: 5
Group 1: null
Group 2: 5
Group 3: null
Full match: x
Group 1: null
Group 2: null
Group 3: x
Full match: ^3.23
Group 1: 3.23
Group 2: null
Group 3: null
Full match: -2012.12
Group 1: null
Group 2: -2012.12
Group 3: null
Full match: x
Group 1: null
Group 2: null
Group 3: x
Full match: 10.12
Group 1: null
Group 2: 10.12
Group 3: null
Full match: -5
Group 1: null
Group 2: -5
Group 3: null
Full match: x
Group 1: null
Group 2: null
Group 3: x
Full match: ^3.54
Group 1: 3.54
Group 2: null
Group 3: null
Full match: 2
Group 1: null
Group 2: 2
Group 3: null
Full match: x
Group 1: null
Group 2: null
Group 3: x
Full match: -1.512
Group 1: null
Group 2: -1.512
Group 3: null


If you wish to simplify/modify/explore the expression, it's been explained on the top right panel of regex101.com. If you'd like, you can also watch in this link, how it would match against some sample inputs.


RegEx Circuit

jex.im visualizes regular expressions:

Example 2

import java.util.regex.Matcher;
import java.util.regex.Pattern;


public class RegularExpression{

    public static void main(String[] args){

        final String regex = "\\^(-?\\d+(?:\\.\\d+)?)|(-?\\d+(?:\\.\\d+)?)|(x)";
        final String string = "12x^11 +10x^9 -8x^7 +9x^6 -5x^3 +2x -1";
        final Pattern pattern = Pattern.compile(regex);
        final Matcher matcher = pattern.matcher(string);

        while (matcher.find()) {
            System.out.println("Full match: " + matcher.group(0));
            for (int i = 1; i <= matcher.groupCount(); i++) {
                System.out.println("Group " + i + ": " + matcher.group(i));
            }
        }

    }
}

Output

Full match: 12
Group 1: null
Group 2: 12
Group 3: null
Full match: x
Group 1: null
Group 2: null
Group 3: x
Full match: ^11
Group 1: 11
Group 2: null
Group 3: null
Full match: 10
Group 1: null
Group 2: 10
Group 3: null
Full match: x
Group 1: null
Group 2: null
Group 3: x
Full match: ^9
Group 1: 9
Group 2: null
Group 3: null
Full match: -8
Group 1: null
Group 2: -8
Group 3: null
Full match: x
Group 1: null
Group 2: null
Group 3: x
Full match: ^7
Group 1: 7
Group 2: null
Group 3: null
Full match: 9
Group 1: null
Group 2: 9
Group 3: null
Full match: x
Group 1: null
Group 2: null
Group 3: x
Full match: ^6
Group 1: 6
Group 2: null
Group 3: null
Full match: -5
Group 1: null
Group 2: -5
Group 3: null
Full match: x
Group 1: null
Group 2: null
Group 3: x
Full match: ^3
Group 1: 3
Group 2: null
Group 3: null
Full match: 2
Group 1: null
Group 2: 2
Group 3: null
Full match: x
Group 1: null
Group 2: null
Group 3: x
Full match: -1
Group 1: null
Group 2: -1
Group 3: null

这篇关于使用正则表达式处理^符号以获取多项式的幂的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆