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 the line above, I need 13.04 and 14.67. I used regex

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

But using this, I get ".13", ".04", ".14", ".67" Thanks in advance

+5
source share
1 answer

Use \\d+ instead of \\d*

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

Why?

Because if you use \\d*\\.\\d+ , this must match the dot that exists next to Rs , since you made the integer part to repeat zero or more times. Therefore, he is not interested in the integer part.

Demo

+7
source

Source: https://habr.com/ru/post/1238266/


All Articles