6

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

1

1 Answer 1

7

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

Sign up to request clarification or add additional context in comments.

5 Comments

See also, the differences between Java regex quantifiers, as documented in the API.
It works fine but can i split this string on behalf of float values ex. CallCost:Rs.13.04 Duration:00:00:02 Bal:Rs.14.67 2016 mein Promotion into["CallCost:Rs.13.04 ","Duration:00:00:02 Bal:Rs.14.67"]
Yes my next action is this
thanks bro...its working perfect..Can you please suggest me any website or tutorials to get regex correspond to requirements.
Nice Answer . Happy coding

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.