Java的;字符串替换(使用正则表达式)?

时间:2009-03-10 20:46:09

标签: java regex

作为学校项目的一部分,我需要从表单中替换字符串:

5 * x^3 - 6 * x^1 + 1

类似于:

5x<sup>3</sup> - 6x<sup>1</sup> + 1

我相信这可以用正则表达式来完成,但我还不知道该怎么做。

你可以伸出援助之手吗?

P.S。实际的分配是实现一个多项式处理Java应用程序,我用它来将多项式.toString()从模型传递给视图,我希望以一种漂亮的方式使用html标签显示它。

12 个答案:

答案 0 :(得分:151)

str.replaceAll("\\^([0-9]+)", "<sup>$1</sup>");

答案 1 :(得分:30)

private String removeScript(String content) {
    Pattern p = Pattern.compile("<script[^>]*>(.*?)</script>",
            Pattern.DOTALL | Pattern.CASE_INSENSITIVE);
    return p.matcher(content).replaceAll("");
}

答案 2 :(得分:15)

String input = "hello I'm a java dev" +
"no job experience needed" +
"senior software engineer" +
"java job available for senior software engineer";

String fixedInput = input.replaceAll("(java|job|senior)", "<b>$1</b>");

答案 3 :(得分:9)

import java.util.regex.PatternSyntaxException;

// (:?\d+) \* x\^(:?\d+)
// 
// Options: ^ and $ match at line breaks
// 
// Match the regular expression below and capture its match into backreference number 1 «(:?\d+)»
//    Match the character “:” literally «:?»
//       Between zero and one times, as many times as possible, giving back as needed (greedy) «?»
//    Match a single digit 0..9 «\d+»
//       Between one and unlimited times, as many times as possible, giving back as needed (greedy) «+»
// Match the character “ ” literally « »
// Match the character “*” literally «\*»
// Match the characters “ x” literally « x»
// Match the character “^” literally «\^»
// Match the regular expression below and capture its match into backreference number 2 «(:?\d+)»
//    Match the character “:” literally «:?»
//       Between zero and one times, as many times as possible, giving back as needed (greedy) «?»
//    Match a single digit 0..9 «\d+»
//       Between one and unlimited times, as many times as possible, giving back as needed (greedy) «+»
try {
    String resultString = subjectString.replaceAll("(?m)(:?\\d+) \\* x\\^(:?\\d+)", "$1x<sup>$2</sup>");
} catch (PatternSyntaxException ex) {
    // Syntax error in the regular expression
} catch (IllegalArgumentException ex) {
    // Syntax error in the replacement text (unescaped $ signs?)
} catch (IndexOutOfBoundsException ex) {
    // Non-existent backreference used the replacement text
}

答案 4 :(得分:8)

"5 * x^3 - 6 * x^1 + 1".replaceAll("\\W*\\*\\W*","").replaceAll("\\^(\\d+)","<sup>$1</sup>");

请注意,在单个正则表达式/替换中加入两个替换项将是一个糟糕的选择,因为x^3 - 6 * x等更通用的表达式会失败。

答案 5 :(得分:3)

如果这是针对任何一般数学表达式并且允许使用括号表达式,那么使用正则表达式执行此操作将非常困难(可能是不可能的)。

如果唯一的替代品是您展示的替代品,那就不难了。首先删除*,然后像CanBerkGüder一样使用捕获来处理^

答案 6 :(得分:3)

你的多项式是什么?如果你正在“处理”它,我正在设想在某些时候生成某种子表达式树,并且会认为用它来生成你的字符串比重新解析raw更简单用正则表达式表达。

只是抛出一种不同的思维方式。我不确定你的应用程序还有什么。

答案 7 :(得分:1)

class Replacement 
{
    public static void main(String args[])
    {
        String Main = "5 * x^3 - 6 * x^1 + 1";
        String replaced = Main.replaceAll("(?m)(:?\\d+) \\* x\\^(:?\\d+)", "$1x<sup>$2</sup>");
        System.out.println(replaced);
    }
}

答案 8 :(得分:0)

你会想要在正则表达式中捕获以处理在^ 3中包装3。

答案 9 :(得分:0)

试试这个:

String str = "5 * x^3 - 6 * x^1 + 1";
String replacedStr = str.replaceAll("\\^(\\d+)", "<sup>\$1</sup>");

请务必导入java.util.regex。

答案 10 :(得分:0)

看看antlr4。与单独的正则表达式相比,它将使您在创建树结构方面更进一步。

https://github.com/antlr/grammars-v4/tree/master/calculator(calculator.g4包含您需要的语法)

简而言之,您定义语法来解析表达式,使用antlr生成Java代码,并添加回调以在构建树时处理评估。

答案 11 :(得分:-1)

试试这个,可能不是最好的方法。但它的工作原理

String str = "5 * x^3 - 6 * x^1 + 1";
str = str.replaceAll("(?x)(\\d+)(\\s+?\\*?\\s+?)(\\w+?)(\\^+?)(\\d+?)", "$1$3<sup>$5</sup>");
System.out.println(str);