Java:如何从正则表达式中解析double [英] Java: how to parse double from regex

查看:343
本文介绍了Java:如何从正则表达式中解析double的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的字符串看起来像A = 1.23; B = 2.345; C = 3.567

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

我只对C = 3.567感兴趣

I am only interested in "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);
    }

问题是它显示3与567分开

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

输出:

3.0

567.0

我想知道我怎么能包含小数,所以它输出3.567

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

编辑:我还想匹配C如果它没有小数点:$ b​​ $ b所以我想捕获3567以及3.567

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 =也被内置到模式中,如何在解析双倍之前将其剥离?

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

推荐答案

我可能在这方面有误,但原因将两者分开是因为 group ()只匹配最后匹配的子序列,即每次调用 find()谢谢,Mark Byers。

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.

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

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+)")

对于解析3567或3.567,您的模式将是 C =(\\d +(\\\\\ +)?) ,组1代表整数。此外,请注意,由于您特别想要匹配句点,因此您希望转义(句点)字符,以便它不会被解释为任意字符标记。但是,对于这个输入,无关紧要

For the parsing 3567 or 3.567, your pattern would be C=(\\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

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

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")

至于从你的模式中取出C =,因为我不熟悉RegExp,我可能会建议你拆分分号上的输入字符串,然后检查每个拆分是否包含C;然后你可以应用模式(使用捕获组)从你的匹配器获得3.567。

As for taking C= out of your pattern, since I'm not that well-versed with RegExp, I might recommend that you split your 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.

编辑更普遍(可能)更有用!)gawi评论中的案例,请使用以下内容(来自 http://www.regular- expressions.info/floatingpoint.html

Edit For the more general (and likely more useful!) cases in gawi's comment, please use the following (from 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.

这篇关于Java:如何从正则表达式中解析double的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆