简体   繁体   中英

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. 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

Use \\\\d+ instead of \\\\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.

DEMO

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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