正则表达式去除尾随零 [英] Regex for removing trailing zeros
本文介绍了正则表达式去除尾随零的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我要寻找一个正规的前pression(.NET)删除尾随零:
11645766.560000001000 - > 11645766.560000001 10190045.740000000000 - > 10190045.74 1455720.820000000100 - > 1455720.8200000001
等等...
我使用正则表达式,在String.Trim(),因为该数字是在一个字符串,实际的例子:
BEGIN>!> C85.18 POS_LEVEL.T129 {11645766.560000001000} = POS_LEVEL.T129 {10190045.740000000000} + WORK_LEVEL.T129 {1455720.820000000100} END;
需要转换为:
BEGIN>!> C85.18 POS_LEVEL.T129 {11645766.560000001} = POS_LEVEL.T129 {10190045.74} + WORK_LEVEL.T129 {1455720.8200000001} END;
解决方案
如果该数字被嵌入在一个随机字符串,正则表达式是要走的路:
替换 (小于?。?= \ \ D +)(?= \ D | $)0 +
有一个空字符串
这将削减尾随零,如果他们在一段时间处于小数后出现(它会永远留下一个零)。它也占出现在最后的号码。
既然你已经添加在此期间,.NET标签,这里的code:
字符串替换= Regex.Replace(
inputString,
@(?。?< = \ \ D +)0 +(?= \ D | $),
的String.Empty);
I am looking for a regular expression (.NET) to remove trailing zeros:
11645766.560000001000 -> 11645766.560000001 10190045.740000000000 -> 10190045.74 1455720.820000000100 -> 1455720.8200000001
etc...
I am using regex, over String.Trim(), because the numbers are in one string, actual example:
!BEGIN !>>C85.18 POS_LEVEL.T129{11645766.560000001000} = POS_LEVEL.T129 {10190045.740000000000} + WORK_LEVEL.T129{1455720.820000000100} END;
need to convert to:
!BEGIN !>>C85.18 POS_LEVEL.T129{11645766.560000001} = POS_LEVEL.T129{10190045.74} + WORK_LEVEL.T129{1455720.8200000001} END;
解决方案
If the numbers are embedded in a random string, regexes are the way to go:
Replace (?<=\.\d+?)0+(?=\D|$)
with an empty string
That will trim trailing zeros if they appear after the period in a decimal number (it will always leave a single zero). It also accounts for numbers that appear at the very end.
And since you've added the .NET tag in the meantime, here's the code:
string replaced = Regex.Replace(
inputString,
@"(?<=\.\d+?)0+(?=\D|$)",
String.Empty);
这篇关于正则表达式去除尾随零的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文