【问题标题】:Regex Pattern with Spaces in JavaJava中带空格的正则表达式模式
【发布时间】:2019-07-07 19:27:24
【问题描述】:

我想确定传递给函数的字符串是有效字符串。这样做时,字符串是一串多项式,它们之间必须有空格。

这些是有效的:

3x^7 3445x^233 3x 34 355
0
+3x^7 x^6 +3445x^233 -3x +34355 x^2

这些无效:

+3x^7+3445x^233-3x +34355
+3x^-7+3445x^233-3x +34355

一个空格不算。每个模式之间都必须有一个空格。如何在不从无效字符串中选择任何项目的情况下选择有效字符串?

我试过这个...

while (str.hasNext()) {
    str.findInLine("([\\+-]*?\\b\\d+)x\\^([\\+-]*?\\d+\\b)"
            + "|([\\+-]*?\\b\\d+)x|([+-]*?\\d+)|\\^(\\d+)");
    MatchResult m = str.match();

    // When the term has a valid coefficient and power ie 3x^3
    if (m.group(1) != null) {
      coefficient = Integer.parseInt(m.group(1));
      power = Integer.parseInt(m.group(2));
      this.addTerm(coefficient, power);
    }
    // When the term ends in x ie 3x
    else if (m.group(3) != null) {
      coefficient = Integer.parseInt(m.group(3));
      this.addTerm(coefficient, 1);
    }
    // When the term has no x ie -3
    else if (m.group(4) != null) {
      coefficient = Integer.parseInt(m.group(4));
      this.addTerm(coefficient, 0);
    }
    // When the term has no coefficient ie x^3
    else if (m.group(5) != null) {
      power = Integer.parseInt(m.group(5));
      this.addTerm(1, power);
    }
}

如您所知,我的正则表达式接受所有有效组而不识别空格。

谢谢!

【问题讨论】:

  • "([+-]?((\\d+x?)|x)(\\^\\d+)?(\\s+|$))+" 按预期验证提供的有效和无效字符串。您可以尝试将字符串按空格拆分并分别分析每个表达式以简化进一步的解析。

标签: java regex regex-negation regex-group regex-greedy


【解决方案1】:

在尝试解决这个问题时,我不小心把它带到了它会解析无效表达式的地方......有效。通过一些简单的逻辑,您可以清除脏表达式,但这可能有效。我会继续看看能不能找到更好的解决方案。

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

public class Main {
    public static void main(String[] args) {
        //Scanner scan = new Scanner("+3x^7 3445x^233 3x 34 355");
        Scanner scan = new Scanner("+3x^7+3445x^233-3x +34355");

    while (scan.hasNext()) {
        String s = scan.next();

        Pattern p = Pattern.compile("((?:[\\+\\-]*?)?\\d+)?x?(?:\\^(\\d+))?");
        Matcher m = p.matcher(s);

        while (m.find())
            System.out.println(m.group());

    }

}

}

这可以用来查看表达式是否有效:

if(!s.matches("(?:[\\+\\-]*\\d*x?\\^?\\d*)") && !s.equals(""))

编辑二

这是我从等式中提取“部分”的示例:

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

public class Main {
public static void main(String[] args) {
    Scanner scan = new Scanner("+3x^7 3445x^233 3x 34 355");
    //Scanner scan = new Scanner("+3x^7+3445x^233-3x +34355");

    while (scan.hasNext()) {
        String s = scan.next();

        if(!s.matches("(?:[\\+\\-]*\\d*x?\\^?\\d*)") && !s.equals(""))
            System.exit(0);

        Pattern p = Pattern.compile("(?:([\\+\\-]*?)?(\\d+))?x?(\\^(\\d+))?");
        Matcher m = p.matcher(s);

        while (m.find())
            for(int i = 0; i < m.groupCount(); i++)
                if(m.group(i) == null)
                    continue;
                else
                    System.out.println(m.group(i));

    }

}

}

输出:

Original:
+3x^7

Parts:
+
3
^7

Original:
3445x^233

Parts:
3445
^233

Original:
3x

Parts:
3

Original:
34

Parts:
34

Original:
355

Parts:
355

我希望这会引导您朝着正确的方向前进。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-09-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多