简体   繁体   English

使用正则表达式拆分数学方程式

[英]Using Regex to split math equations

So I am trying to split a simple mathematical equation containing two doubles in scientific notation, eg "54E-1-57E-1". 因此,我试图拆分一个简单的数学方程,该科学方程以科学计数形式包含两个双精度,例如“ 54E-1-57E-1”。 I'm trying to find regex pattern to do this for me, but I have come across two problems. 我正在尝试寻找正则表达式模式来为我完成此任务,但是遇到了两个问题。 The first is, the following code splits my string successfully but for some reason negTest prints out false, and I have no idea why. 首先是,下面的代码成功地分割了我的字符串,但是由于某种原因negTest打印出了false,我也不知道为什么。

public static void main(String[] args) {
    String testString = "54E-1-57E-1";
    boolean negTest = string.contains("(?<=\\d)-(?=\\d)");
    System.out.println(negTest);
    String[] output = testString.split("(?<=\\d)-(?=\\d)");
    System.out.println(Arrays.toString(output));
}

//Output:
//false
//[54E-1, 57E-1]

I assume it is a problem with my regex, but the string splits successfully around the minus which confuses me. 我以为这是我的正则表达式的问题,但是字符串成功地在负号附近拆分,这使我感到困惑。 The second problem is how would I split it so that the minus is included in the second term, eg 第二个问题是我将如何拆分,以便在第二项中包含减号,例如

[54E-1, -57E-1]

Any help/explanations are greatly appreciated! 任何帮助/解释都非常感谢!

1> contains doesn't take regex as a parameter..So you are trying to match (?<=\\\\d)-(?=\\\\d) as a string not regex 1> contains不将正则表达式作为参数。因此,您尝试将(?<=\\\\d)-(?=\\\\d)匹配为不是正则表达式的字符串

2>To include - ,your regex should be 2>要包含- ,您的正则表达式应为

(?<=\\d)(?=-\\d)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM