[英]Java; String replace (using regular expressions)?
作为学校项目的一部分,我需要从表单中替换一个字符串:
5 * x^3 - 6 * x^1 + 1
类似于:
5x<sup>3</sup> - 6x<sup>1</sup> + 1
我相信这可以用正则表达式来完成,但我还不知道该怎么做。
你能借给我一个手吗?
PS实际的分配是实现一个多项式处理Java应用程序,我用它来将多项式.toString()从模型传递给视图,我希望以一种漂亮的方式使用html标签来显示它。
str.replaceAll("\\^([0-9]+)", "<sup>$1</sup>");
private String removeScript(String content) {
Pattern p = Pattern.compile("<script[^>]*>(.*?)</script>",
Pattern.DOTALL | Pattern.CASE_INSENSITIVE);
return p.matcher(content).replaceAll("");
}
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>");
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
}
"5 * x^3 - 6 * x^1 + 1".replaceAll("\\W*\\*\\W*","").replaceAll("\\^(\\d+)","<sup>$1</sup>");
请注意,在单个正则表达式/替换中加入两个替换将是一个糟糕的选择,因为更通用的表达式,如x^3 - 6 * x
将失败。
如果这是针对任何通用数学表达式并且允许使用括号表达式,则使用正则表达式执行此操作将非常困难(可能不可能)。
如果唯一的替代品是你展示的那些,那就不难了。 首先剥离*
,然后像CanBerkGüder一样使用捕获来处理^
。
你的多项式是什么? 如果你正在“处理”它,我正在设想在某些时候生成某种子表达式树,并且会认为用它来生成你的字符串比重新解析raw更简单用正则表达式表达。
只是抛出一种不同的思维方式。 我不确定你的应用程序还有什么。
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);
}
}
试试这个,可能不是最好的方法。 但它的确有效
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);
看看antlr4。 与单独的正则表达式相比,它将使您在创建树结构方面更进一步。
https://github.com/antlr/grammars-v4/tree/master/calculator ( calculator.g4包含您需要的语法)
简而言之,您定义语法来解析表达式,使用antlr生成Java代码,并添加回调以在构建树时处理评估。
你会想要在正则表达式中捕获以处理包裹3 in ^ 3。
试试这个:
String str = "5 * x^3 - 6 * x^1 + 1";
String replacedStr = str.replaceAll("\\^(\\d+)", "<sup>\$1</sup>");
一定要导入java.util.regex。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.