开发者

Remove all zeroes except in numbers

开发者 https://www.devze.com 2023-03-25 12:56 出处:网络
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 \'-

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:

string clean = Regex.Replace(dirty, @"(?<!\d)0+|0+(?!\d|$)", "");


If I understand your pattern correctly, the following should work.

using System;
using System.Collections.Generic;
using System.Text.RegularExpressions;

public class Test
{
        public static void Main()
        {
                List<String> samples = new List<String>(new[]{
                        "0A055","0A050","0A500","0A0505","0055","0505","0050"
                });

                String re = @"^0*([A-Z]*)0*([1-9]\d*)$";

                // iterate over all results
                samples.ForEach(n => {
                        Console.WriteLine("\"{0}\" -> \"{1}\"",
                                n,
                                Regex.Replace(n, re, "$1$2")
                        );
                });
        }
}

With the following output:

"0A055" -> "A55"
"0A050" -> "A50"
"0A500" -> "A500"
"0A0505" -> "A505"
"0055" -> "55"
"0505" -> "505"
"0050" -> "50"

Basically use the pattern to negate all 0s that don't matter, and use the regex replace grouping to re-concatenate the "meaningful" numbers (and letters when present).


Like some of the others I'm not sure regex is the best idea here, but this works with the test cases:

0+(?=[0-9].)|0(?=[a-zA-z])|(?<=[a-zA-Z])0+


Since you seem to only have one letter, you can split the string in two halves on that letter.

On the left part, trim all zeros.

On the right part, convert it to a number, this will drop all leading zeros or you could use TrimStart.


To do a replace with regex will be much harder than extracting the value you want. So try match the string using a simple regex like below

0*(?<letter>[A-Z])0*(?<number>\d*)

Your match result will then contain two groups, letter and number. Take the value of the two group and append them and you will get what you wanted.


Here's a Perl answer for what it's worth

s/0*([a-zA-Z]*)0*([1-9]+0*)/$1$2/g


I don't know how regex is implemented in .net, so I'll let you write the proper code with the toys in System.Text.Regularexpressions.Regex (MSDN)

Either way, this pattern should work (in pseudo-code):

Replace "(0*)(.+)" by "$2"

0* means zero or more 0

.+ means any character except end of line

$2 represents the second set of brackets (so we're simply discarding the (0*) part of the string).

0

精彩评论

暂无评论...
验证码 换一张
取 消