简体   繁体   English

使用Java中的正则表达式从字符串中提取双精度或整数?

[英]extract a double or integer from a string using regular expression in java?

i want to extract an equation constants(ie integer or double NOT followed by a letter like say "x") from a string. 我想从字符串中提取方程常数(即整数或双NOT,后跟一个类似“ x”的字母)。 eg eqn = "30.14x^2 + 55.69x + 60.1 = 100" . 例如eqn = "30.14x^2 + 55.69x + 60.1 = 100" So far, i can get the coefficients of x using 到目前为止,我可以使用来获得x的系数

([\\+\\-])?(\\d+(\\.\\d+)?)x

which gives 55.69x or 30.14x in this case. 在这种情况下为55.69x or 30.14x

Now i want to get the constants and I've tried regex below to extract +60.1 or 100 现在我想获取常量,我已经尝试过下面的正则表达式来提取+60.1或100

(?<!^)([\\+\\-])?(\\d+(\\.\\d+)?)(?!x)

However this doesn't work.Any help would be highly appreciated since I've on this for a month or so. 但这是行不通的,因为我已经做了一个月左右的时间,所以任何帮助将不胜感激。

what about 关于什么

 (?<!\^)(-|\+)?[\d.]++(?!x)

?

this variant uses the possessive quantifier ( the "++"), may be less strict with invalid numbers, but if the strings are syntactically correct, this does not matter. 此变体使用所有格修饰符(“ ++”),对于无效数字可能不太严格,但是如果字符串在语法上是正确的,则没关系。

This should work: 这应该工作:

([\\+\\-])?(\\d+(\\.\\d+)?)(?![\\d.x])

[\\\\dx] works because it eliminates partial numbers. [\\\\dx]之所以有效,是因为它消除了部分数字。 Eg: For 30.14x, 30.14 wouldn't match (?!x) but 30.1 would. 例如:对于30.14x,30.14不匹配(?!x)但匹配30.1。

Here's a way to extract those values in a couple of passes: 这是通过两次传递来提取这些值的方法:

public static void main(String[] args) {

    String eqn = "30.14x^2 + 55.69x + 60.1 = 100";

    String numberRegex = "[\\d]+(?:[\\.]{1}[\\d]+)?";
    String symbolsRegex = "=+-/\\\\*\\^";

    String coefRegex = "("+numberRegex+")([a-z]{1})";
    Pattern p = Pattern.compile(coefRegex);
    Matcher m = p.matcher(eqn);
    while (m.find()) {
        System.out.println("coefficient: " + m.group(1) + " -- " + m.group(2));
    }

    String constRegexp = "([^ " + symbolsRegex + "]" + numberRegex + "(?:[ ]{1}|$))";
    p = Pattern.compile(constRegexp);
    m = p.matcher(eqn);
    while (m.find()) {
        System.out.println("constant: " + m.group(1));
    }

}

Output: 输出:

coefficient: 30.14 -- x
coefficient: 55.69 -- x
constant: 60.1 
constant: 100

Just to add the literal values of the regex I used above: 只是添加我上面使用的正则表达式的文字值:

String coefRegex = "([\\d]+(?:[\\.]{1}[\\d]+)?)([a-z]{1})";
String constRegexp = "([^ =+-/\\\\*\\^][\\d]+(?:[\\.]{1}[\\d]+)?(?:[ ]{1}|$))";

What is wrong with your regex is that you did not escape ^ , thus it means beginning of the string. 正则表达式的问题在于您没有对^转义,因此它意味着字符串的开头。 And you also had not accounted for the substrings preceded by a digit or a decimal-point. 而且,您也没有考虑到以数字或小数点开头的子字符串。 Neither for the ones proceeded by one. 没有一个人继续前进。 Hence the correct form of your regex would be: 因此,您的正则表达式的正确形式为:

(?<!(\\^|\\d|\\.))[+-]?(\\d+(\\.\\d+)?)(?!(x|\\d|\\.))

which works ok. 可以的。 And also if you care about the exponent on x you might wanna go like this: 而且,如果您关心x上的指数,您可能想要这样做:

([+-])?(\\d+(\\.\\d+)?)x(^(\d+))?

by the way, you don't need to escape + inside [] , and - when it is next to [ of ] . 顺便说一下,你不需要逃避+[]并且-当它旁边的[]

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

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