Java:如何从正则表达式解析双精度

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3681242/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-14 03:27:42  来源:igfitidea点击:

Java: how to parse double from regex

javaregex

提问by Will

I have a string that looks like "A=1.23;B=2.345;C=3.567"

我有一个看起来像“A=1.23;B=2.345;C=3.567”的字符串

I am only interested in "C=3.567"

我只对“C=3.567”感兴趣

what i have so far is:

到目前为止我所拥有的是:

     Matcher m = Pattern.compile("C=\d+.\d+").matcher("A=1.23;B=2.345;C=3.567");

    while(m.find()){ 
        double d = Double.parseDouble(m.group());
        System.out.println(d);
    }

the problem is it shows the 3 as seperate from the 567

问题是它显示 3 与 567 分开

output:

输出:

3.0

3.0

567.0

567.0

i am wondering how i can include the decimal so it outputs "3.567"

我想知道如何包含小数,以便输出“3.567”

EDIT: i would also like to match C if it does not have a decimal point: so i would like to capture 3567 as well as 3.567

编辑:如果 C 没有小数点,我也想匹配它:所以我想捕获 3567 和 3.567

since the C= is built into the pattern as well, how can i strip it out before parsing the double?

由于 C= 也内置在模式中,我如何在解析双精度之前将其去掉?

采纳答案by Brian

I may be mistaken on this part, but the reason it's separating the two is because group()will only match the last-matched subsequence, which is whatever gets matched by each call to find().Thanks, Mark Byers.

我可能在这方面弄错了,但将两者分开的原因是group()只会匹配最后匹配的子序列,这是每次调用find()时匹配的任何内容。谢谢,马克拜尔斯。

For sure, though, you can solve this by placing the entire part you want inside a "capturing group", which is done by placing it in parentheses. This makes it so that you can group together matched parts of your regular expression into one substring. Your pattern would then look like:

不过,可以肯定的是,您可以通过将所需的整个部分放在一个“捕获组”中来解决这个问题,这是通过将它放在括号中来完成的。这使得您可以将正则表达式的匹配部分组合成一个子字符串。您的模式将如下所示:

Pattern.compile("C=(\d+\.\d+)")

For the parsing 3567 or 3.567, your pattern would beC=(\\d+(\\.\\d+)?)with group 1 representing the whole number. Also, do note that since you specifically want to match a period, you want to escape your .(period) character so that it's not interpreted as the "any-character" token. For this input, though, it doesn't matter

对于解析 3567 或 3.567,您的模式将C=(\\d+(\\.\\d+)?)使用代表整数的组 1。另外,请注意,由于您特别想要匹配一个句点,您想要转义您的.(句点)字符,以便它不会被解释为“任何字符”标记。不过,对于这个输入,这并不重要

Then, to get your 3.567, you would you would call m.group(1)to grab the first (counting from 1) specified group. This would mean that your Double.parseDouble call would essentially become Double.parseDouble("3.567")

然后,要获得 3.567,您会打电话给 m。group(1)获取第一个(从 1 开始计数)指定的组。这意味着你的 Double.parseDouble 调用基本上会变成Double.parseDouble("3.567")

As for taking C= out of your pattern, since I'm not that well-versed with RegExp, I might recommend that you splityour input string on the semi-colons and then check to see if each of the splits contain the C; then you could apply the pattern (with the capturing groups) to get the 3.567 from your Matcher.

至于将 C= 从你的模式中去掉,因为我对 RegExp 不是那么精通,我可能建议你在分号上拆分输入字符串,然后检查每个拆分是否包含 C;然后您可以应用模式(使用捕获组)从您的匹配器中获取 3.567。

EditFor the more general (and likely more useful!) cases in gawi's comment, please use the following (from http://www.regular-expressions.info/floatingpoint.html)

编辑对于 gawi 评论中更一般(可能更有用!)的情况,请使用以下内容(来自http://www.regular-expressions.info/floatingpoint.html

Pattern.compile("[-+]?[0-9]*\.?[0-9]+([eE][-+]?[0-9]+)?")

This has support for optional sign, either optional integer or optional decimal parts, and optional positive/negative exponents. Insert capturing groups where desired to pick out parts individually. The exponent as a whole is in its own group to make it, as a whole, optional.

这支持可选符号,可选整数或可选小数部分,以及可选的正/负指数。在需要单独挑选零件的地方插入捕获组。指数作为一个整体在它自己的组中,使其作为一个整体是可选的。

回答by Mark Byers

To match any sequence of digits and dots you can change the regular expression to this:

要匹配任何数字和点序列,您可以将正则表达式更改为:

"(?<=C=)[.\d]+"

If you want to be certain that there is only a single dot you might want to try something like this:

如果你想确定只有一个点,你可能想尝试这样的事情:

"(?<=C=)\d+(?:\.\d+)?"

You should also be aware that this pattern can match the 1.2in ABC=1.2.3;. You should consider if you need to improve the regular expression to correctly handle this situation.

您还应该知道,此模式可以匹配1.2in ABC=1.2.3;。您应该考虑是否需要改进正则表达式以正确处理这种情况。

回答by Shadwell

Your regular expression is only matching numeric characters. To also match the decimal point too you will need:

您的正则表达式仅匹配数字字符。要也匹配小数点,您将需要:

Pattern.compile("\d+\.\d+")

The .is escaped because this would match any character when unescaped.

.被转义,因为这将转义当匹配任何字符。

Note: this will then only match numbers with a decimal point which is what you have in your example.

注意:这将只匹配带有小数点的数字,这就是您在示例中所拥有的。

回答by august0490

if you need to validate decimal with dots, commas, positives and negatives:

如果您需要使用点、逗号、正数和负数来验证小数:

Object testObject = "-1.5";
boolean isDecimal = Pattern.matches("^[\+\-]{0,1}[0-9]+[\.\,][0-9]+$", (CharSequence) testObject);

Good luck.

祝你好运。