我要寻找一个正规的前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;
![java正则表达式 mysky221](https://p.xsw88.cn/allimgs/daicuo/20230903/5587.png)
解决方案
如果该数字被嵌入在一个随机字符串,正则表达式是要走的路:
替换 (小于?。?= 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);
相关推荐
最新文章