1

This is my regex:

((((?<=(\d\.)(([1-9]))))|((?<=\d)\.[0])))((0)+) 

This is my Description:

af 0.044 00.22 44.3500,44.334000,0.5000,1.750,1.9000,0.1480,10.2500,0.7480",19.00 MM,62.00MM,12MM,12.0000MM,DRILL BIT,0.040',1.00 IN MINI BEND RADIUS|0.3120IN TIP DIA|36.00 IN LG,45MM,45.0000MM,30MM,30.0000MM,40MM,40.0000MM,40.00 15.20,40.00,15.20000:14.00,40.00;15.20000*14.00X40.00/15.20000(14.00),40.00\15.20000{14.00},40.00|15.20000[14.00],40.00?15.20000%14.00,15.20000{14.00},40.00|15.20000,15.20000%14.00, 345.00 354.34500aa 354.aa 354. ABC.000 17.0

As per requirement it should removing trailing 0's from the description.The above regex is removing the zeros but failing in some conditions. Like its failing on if value is 0.040, can anyone suggest me a better regex which would satisfy all the conditions.

9
  • 1
    What's the desired output? Commented Jun 17, 2016 at 9:02
  • Try replaceAll("\\.?0+(?![0-9.])", "") Commented Jun 17, 2016 at 9:12
  • I need to build a regex which should be able to remove all the trailing 0s where evere present in the description Commented Jun 17, 2016 at 9:16
  • @AbhayKant: But you need to precise. What about 354.? Is ABC.000 to be touched? Please post the expected output. Commented Jun 17, 2016 at 9:16
  • This is working fine but failing in condition if value is:- ABC.00 Commented Jun 17, 2016 at 9:25

2 Answers 2

1
s = s.replaceAll("(\\d\\.\\d*[1-9])0+\\b", "$1")
     .replaceAll("(\\d)\\.0+\\b", "$1");

For the two cases 1.034000 to 1.034 and 1.000

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

Comments

1

Based on the expected results of your comments you just want to remove the .0 from decimals that end with them. In that case this should work:

s = s.replaceAll("([0-9])+[.][0](?![0-9])", "$1");

Hence : 123.0 becomes 123

To do a bit more thourough 0-cleaning. A bit of lazy matching and a negative lookahead could do the job.

0*([0-9]+[.][0-9][0-9]*?)0+(?![0-9])

For this test example:

String s = "0.010 00.20200,003.30330;10044.04400M,5.0";
s = s.replaceAll("0*([0-9]+[.][0-9][0-9]*?)0+(?![0-9])", "$1");

s will return:

0.01 0.202,3.3033;10044.044M,5.0

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.