Truncate trailing zeroes regex

a 夏天 提交于 2020-01-15 10:53:28

问题


How to get a match using regex to truncate zeroes in a string like this

45.9390 => 45.939
32.00 => 32.0 [Need this for my use case]
32.0050 => 32.005
32 => 32

I am using Java. I am just looking for the regex. This is my closest attempt( https://regex101.com/r/mD7gK4/79 ). I am missing on the second case.


回答1:


I have edited your regex:

^([+-]?\d*\.0)0+$|^([+-]?\d*\.?\d*?)0*$

and use \1\2 as substitution string:

Demo: https://regex101.com/r/mD7gK4/81

Output:

45.9390 => 45.939
32.00 => 32.0
32.0050 => 32.005
32 => 32


来源:https://stackoverflow.com/questions/48981702/truncate-trailing-zeroes-regex

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!