• Post Reply Bookmark Topic Watch Topic
  • New Topic

Decimal point remove  RSS feed

 
Shiva Mohan
Ranch Hand
Posts: 486
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If I have 0123.45 string and trying to get 12345 as result. I am trying regex like below.

String s="0123.45";
s = s.replaceAll("^0*([0-9]+).*", "$1");

It gave result 123 as leading zeros removed that is one of the things that I want. How do I achieve 12345, please?
 
Ron McLeod
Bartender
Posts: 1602
232
Android Angular Framework Eclipse IDE Java Linux MySQL Database Redhat TypeScript
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I don't have the answer, but there is an on-line playground here which might be useful to test your regex patterns:

http://www.regexplanet.com/advanced/java/index.html
 
Jayesh A Lalwani
Rancher
Posts: 2762
32
Eclipse IDE Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You use regex to solve a problem, now you have 2 problems

The $1 refers to the first match in your regex. In this case it refers to whatever matched ^0* which is 123. If you want more matches you will have to put them in your replace expression


I would suggest that you build your regex step by step. Don't try to build the whole thing in one go. Your regex doesn't do what you think it does. First focus on finding the dot. It seems to me that you can simplify you regex if you just focus on replacing the dot with nothing rather than trying to extract numbers out of it.

 
Shiva Mohan
Ranch Hand
Posts: 486
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the reply. I am new to regex and learning it with fixing this. Is there any other way to fix this using DecimalFormat or NumberFormat or anything else rather than regex?
 
Paweł Baczyński
Bartender
Posts: 2077
44
Firefox Browser IntelliJ IDE Java Linux Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Shiva Mohan wrote:Thanks for the reply. I am new to regex and learning it with fixing this. Is there any other way to fix this using DecimalFormat or NumberFormat or anything else rather than regex?

Well you don't need to use regex at all. To remove a dot just use s = s.replace(".", ""); .
Then remove leading zeroes in any way you want. It might be replaceAll using regex. It might be a loop. Or any other rhing that works.
 
Shiva Mohan
Ranch Hand
Posts: 486
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the help. I have removed leading zeros using DecimalFormat.
 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Shiva Mohan wrote:If I have 0123.45 string and trying to get 12345 as result. I am trying regex like below.

String s="0123.45";
s = s.replaceAll("^0*([0-9]+).*", "$1");

It gave result 123 as leading zeros removed that is one of the things that I want. How do I achieve 12345, please?



You can remove leading zeros and (all) the decimal point(s), with a slight code change.



Henry


PS... Normally, I don't just give out an answer -- but it looks like the OP worked out the solution, along with discussions of alternate solutions. So, this is just for being complete.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!