简体   繁体   English

如何使用java中的REGEX从字符串解析浮点值

[英]How to parse float values from string using REGEX in java

I want to parse float values from 我想从中解析浮点值

CallCost:Rs.13.04 Duration:00:00:02 Bal:Rs.14.67 2016 mein Promotion

From above string i need 13.04 and 14.67. 从上面的字符串我需要13.04和14.67。 I used following regex 我使用了以下正则表达式

  Pattern p = Pattern.compile("\\d*\\.\\d+");
  Matcher m = p.matcher(s);
  while (m.find()) {
  System.out.println(">> " + m.group());
            }

But using this i am getting ".13", ".04", ".14", ".67" Thanks in advance 但使用这个我得到“.13”,“。04”,“。14”,“。67”在此先感谢

Use \\\\d+ instead of \\\\d* 使用\\\\d+而不是\\\\d*

 Pattern p = Pattern.compile("\\d+\\.\\d+");

Why? 为什么?

Because if you use \\\\d*\\\\.\\\\d+ , this should match from the dot exists next to Rs , since you made the integer part to repeat zero or more times., So it don't care about the integer part. 因为如果使用\\\\d*\\\\.\\\\d+ ,这应该与Rs旁边的点匹配,因为你使整数部分重复零次或多次。所以它不关心整数部分。

DEMO DEMO

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

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