I have to remove all the zeroes in a string, but I have to keep the zeroes in numbers.
The strings I receive are in a format similar to “zeroes-letter-zeroes-number”, without the ‘-‘ and the numbers are always integers. A few examples:
"0A055" -> "A55"
"0A050" -> "A50"
"0A500" -> "A500"
"0A0505" -> "A505"
"0055" -> "55"
"0505" -> "505"
"0050" -> "50"
I know I can iterate trough the characters in the string and set a flag when I encounter a letter or a number different from 0, but I think that using a RegEx would nicer. The RegEx would also be more helpful if I’ll have to use this algorithm in the database.
I tried something like this but I don’t get the results that I want:
Regex r = new Regex(@"[0*([a-zA-Z]*)0*([1-9]*)]");
string result = r.Replace(input, "");
I’m not so good in writing RegEx-es so please help me if you can.
I’m not convinced that a regex is the best way to approach this, but this one works with all your test cases: