tags:

views:

1261

answers:

5

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;
+2  A: 

In .Net you can do the following

var newStr = Regex.Replace(input, "0+$",String.Empty);
JaredPar
+1 - The original question vs. the edited one are completely different questions. You didn't deserve the down vote.
Sean Bright
A: 
([0]+)\b

then delete $1

Pod
+1 - The original question vs. the editted one are completely different questions. You didn't deserve the down vote.
Sean Bright
That's one of the problems with SO.
Philippe Leybaert
in my limited experience the '.' is considered a word boundary character making this implementation delete a zero before the point.
Victor
Victor's right. Instead of \b, I would suggest (?![.\d]) - "not followed by a dot or a digit". At least, until the OP changes the requirements again. ;)
Alan Moore
But you don't need to capture the zeroes and explicitly delete them. Just replace the matched text with an empty string, like activa did.
Alan Moore
+10  A: 

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);
Philippe Leybaert
This should work. Good job.
Steve Wortham
Downvoters: please explain
Philippe Leybaert
I tested all of the sample numbers with the Replace feature at http://regexhero.net/ and your regex works perfectly.
Steve Wortham
Oh well, I guess whoever downvoted doesn't like the idea that a regex could be the best solution to solve this problem
Philippe Leybaert
Where does it fail? I've tested it and I can't see anything wrong with it. Can you be more specific?
Philippe Leybaert
@activa - You're right, it's all good.
Gavin Miller
i'm under the impression that you can't use the + or * characters in the lookbehinds. also when trying your regex at gskinner.com/regexr it doesn't work. do some regex's have different standards?fyi i'm not downvoter.
Victor
They certainly work in .NET regular expressions. I don't know about other implementations.
Philippe Leybaert
You can use + or * in lookbehinds with the .NET regular expression engine. gskinner.com/regexr doesn't use the .NET engine though. But regexhero.net does.
Steve Wortham
I suspect part of the reason for the downvotes is that we couldn't see the first backslash in the lookbehind; you should ALWAYS use backticks or indented code blocks to format regexes.
Alan Moore
A: 

One caveat on the above RegEx AND Trim()... if your input does NOT have a decimal point, you could end up with wrong numbers. E.g., ("1000").TrimEnd('0') = 1.

This workaround will fix that issue:

string Trimmed = (Untrimmed.IndexOf('.')>=0) ? Untrimmed.TrimEnd('0') : Untrimmed;

Of course, the use of a "." for decimals is culture-specific--some cultures use commas. If you're trying to built in culture-awareness, you need to use System.Threading.Thread.CurrentThread.CurrentCulture.NumberDecimalSeparator instead of a literal '.' character.

Edit: Based on your edited question above, RegEx is the right answer. Something like:

Trimmed = RegEx.Replace(Untrimmed, "(\d\.\d*[1-9])0+([^\d]|$)", "$1$2");

(The [1-9] above is my lazy way to avoid \d being greedy and capturing the 0's, there's likely a more straightforward way to do it.)

richardtallent
A: 

The following will match zeros that follow a decimal point and any number of decimals, and that do not precede a decimal point or digit.

(?<=\.\d*)(0+)(?=[^.\d])
CptSkippy
Because it doesn't address the question asked.The questioner asked for something that did this:11645766.560000001000 -> 11645766.56000000110190045.740000000000 -> 10190045.741455720.820000000100 -> 1455720.8200000001 According to The Regulator, yours does this:11645766.560000001000 -> .560000001000 10190045.740000000000 -> .740000000000 1455720.820000000100 ->
Rob Levine
That makes sense. :)
CptSkippy