正则表达式如何分隔多项式的项

我有一个字符串5x^3-2x^2+5x 我想要一个正则表达式,将该字符串拆分为 5x^3, -2x^2, 5x

我尝试过"(-)|(\\+)",但这没有用。因为它没有考虑负幂项。


温温酱
浏览 121回答 3
3回答

慕仙森

您可以使用此正则表达式分割字符串,\+|(?=-)它的工作方式是,它分割字符串消耗+字符,但如果有的话-它会分割使用-但不消耗-,因为这是向前看的。看看这个Java代码,String s = "5x^3-2x^2+5x";System.out.println(Arrays.toString(s.split("\\+|(?=-)")));下面给出了您的预期输出,[5x^3, -2x^2, 5x]编辑:尽管在OP在他的帖子中的评论之一中他说,不会有负权力,但以防万一你也有负权力,你可以使用这个正则表达式来处理负权力,\+|(?<!\^)(?=-)检查这个更新的 Java 代码,List<String> list = Arrays.asList("5x^3-2x^2+5x", "5x^3-2x^-2+5x");for (String s : list) {&nbsp; &nbsp; System.out.println(s + " --> " +Arrays.toString(s.split("\\+|(?<!\\^)(?=-)")));}新输出,5x^3-2x^2+5x --> [5x^3, -2x^2, 5x]5x^3-2x^-2+5x --> [5x^3, -2x^-2, 5x]

POPMUISE

或许,-?[^\r\n+-]+(?=[+-]|$)或者一些类似的表达式也可能可以正常工作,以防万一方程中可能有常数。演示测试import java.util.regex.Matcher;import java.util.regex.Pattern;public class RegularExpression{&nbsp; &nbsp; public static void main(String[] args){&nbsp; &nbsp; &nbsp; &nbsp; final String regex = "-?[^\r\n+-]+(?=[+-]|$)";&nbsp; &nbsp; &nbsp; &nbsp; final String string = "5x^3-2x^2+5x\n"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;+ "5x^3-2x^2+5x-5\n"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;+ "-5x^3-2x^2+5x+5";&nbsp; &nbsp; &nbsp; &nbsp; final Pattern pattern = Pattern.compile(regex, Pattern.MULTILINE);&nbsp; &nbsp; &nbsp; &nbsp; final Matcher matcher = pattern.matcher(string);&nbsp; &nbsp; &nbsp; &nbsp; while (matcher.find()) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.println("Full match: " + matcher.group(0));&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; for (int i = 1; i <= matcher.groupCount(); i++) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.println("Group " + i + ": " + matcher.group(i));&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }}如果您希望简化/修改/探索表达式,请在regex101.com的右上角面板上进行解释。如果您愿意,您还可以在此链接中观看它如何与某些示例输入进行匹配。正则表达式电路jex.im可视化正则表达式:

12345678_0001

在下面的程序中,您可以中断每个变量。因此,请对其进行调试并根据需要组合正则表达式。它适用于所有输入。import java.util.regex.*;class Main{&nbsp; public static void main(String[] args)&nbsp; {&nbsp; &nbsp; String txt="5x^3-2x^2+5x";&nbsp; &nbsp; String re1="([-+]\\d+)";&nbsp; &nbsp; // Integer Number 1&nbsp; &nbsp; String re2="((?:[a-z][a-z0-9_]*))"; // Variable Name 1&nbsp; &nbsp; String re3="(\\^)"; // Any Single Character 1&nbsp; &nbsp; String re4="([-+]\\d+)";&nbsp; &nbsp; // Integer Number 2&nbsp; &nbsp; String re5="([-+]\\d+)";&nbsp; &nbsp; // Integer Number 1&nbsp; &nbsp; String re6="((?:[a-z][a-z0-9_]*))"; // Variable Name 2&nbsp; &nbsp; String re7="(\\^)"; // Any Single Character 2&nbsp; &nbsp; String re8="([-+]\\d+)";&nbsp; &nbsp; // Integer Number 3&nbsp; &nbsp; String re9="([-+]\\d+)";&nbsp; &nbsp; // Integer Number 2&nbsp; &nbsp; String re10="((?:[a-z][a-z0-9_]*))";&nbsp; &nbsp; // Variable Name 3&nbsp; &nbsp; Pattern p = Pattern.compile(re1+re2+re3+re4+re5+re6+re7+re8+re9+re10,Pattern.CASE_INSENSITIVE | Pattern.DOTALL);&nbsp; &nbsp; Matcher m = p.matcher(txt);&nbsp; &nbsp; if (m.find())&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; String int1=m.group(1);&nbsp; &nbsp; &nbsp; &nbsp; String var1=m.group(2);&nbsp; &nbsp; &nbsp; &nbsp; String c1=m.group(3);&nbsp; &nbsp; &nbsp; &nbsp; String int2=m.group(4);&nbsp; &nbsp; &nbsp; &nbsp; String signed_int1=m.group(5);&nbsp; &nbsp; &nbsp; &nbsp; String var2=m.group(6);&nbsp; &nbsp; &nbsp; &nbsp; String c2=m.group(7);&nbsp; &nbsp; &nbsp; &nbsp; String int3=m.group(8);&nbsp; &nbsp; &nbsp; &nbsp; String signed_int2=m.group(9);&nbsp; &nbsp; &nbsp; &nbsp; String var3=m.group(10);&nbsp; &nbsp; &nbsp; &nbsp; System.out.print("("+int1.toString()+")"+"("+var1.toString()+")"+"("+c1.toString()+")"+"("+int2.toString()+")"+"("+signed_int1.toString()+")"+"("+var2.toString()+")"+"("+c2.toString()+")"+"("+int3.toString()+")"+"("+signed_int2.toString()+")"+"("+var3.toString()+")"+"\n");&nbsp; &nbsp; }&nbsp; }}
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java