简体   繁体   中英

How to remove numbers from string using Regex.Replace?

I need to use Regex.Replace to remove all numbers and signs from a string.

Example input: 123- abcd33
Example output: abcd

Try the following:

var output = Regex.Replace(input, @"[\d-]", string.Empty);

The \\d identifier simply matches any digit character.

You can do it with a LINQ like solution instead of a regular expression:

string input = "123- abcd33";
string chars = new String(input.Where(c => c != '-' && (c < '0' || c > '9')).ToArray());

A quick performance test shows that this is about five times faster than using a regular expression.

var result = Regex.Replace("123- abcd33", @"[0-9\-]", string.Empty);

Blow codes could help you...

Fetch Numbers:

return string.Concat(input.Where(char.IsNumber));

Fetch Letters:

return string.Concat(input.Where(char.IsLetter));

As a string extension:

    public static string RemoveIntegers(this string input)
    {
        return Regex.Replace(input, @"[\d-]", string.Empty);
    }

Usage:

"My text 1232".RemoveIntegers(); // RETURNS "My text "

the best design is:

public static string RemoveIntegers(this string input)
    {
        return Regex.Replace(input, @"[\d-]", string.Empty);
    }
text= re.sub('[0-9\n]',' ',text)

在 python 中安装正则表达式,然后执行以下代码。

Different methods and which is the fastest if you have 100000 iterations to do.

Code:

        Stopwatch sw = new Stopwatch();
        var maxIterations = 100000;

        Console.WriteLine(@"Removing digits from string: ""1mir1112211a3bc9"" with Total {0}x iterations ",maxIterations);
        Console.WriteLine("\nReplace Operations");
        sw.Start();
        var str = "1mir1112211a3bc9";
        for (int i = 1; i <= maxIterations; i++)
        {
            str = "1mir1112211a3bc9";

            str = str.Replace("1", "")
                     .Replace("2", "")
                     .Replace("3", "")
                     .Replace("4", "")
                     .Replace("5", "")
                     .Replace("6", "")
                     .Replace("7", "")
                     .Replace("8", "")
                     .Replace("9", "")
                     .Replace("0", "");
        }
        sw.Stop();
        
        Console.WriteLine("Finalstring: " + str);
        Console.WriteLine("Elapsed time: " + sw.Elapsed.TotalMilliseconds + " Milliseconds");

        sw.Reset();

        //list for and if 
        Console.WriteLine("\nList Operations:");
        sw.Start();
        var str2 = "1mir1112211a3bc9";
        var listOfchars = new List<char>();
        for (int i = 1; i <= maxIterations; i++)
        {
             str2 = "1mir1112211a3bc9";
            for (int j = 0; j < str2.Length; j++)
            {
                if( !(char.IsDigit(str2[j])))
                    listOfchars.Add(str2[j]);
            }
            str2 = new string(listOfchars.ToArray());
            listOfchars.Clear();
        }
        sw.Stop();
        Console.WriteLine("Finalstring: " + str2);
        Console.WriteLine("Elapsed time: " + sw.Elapsed.TotalMilliseconds + " Milliseconds");

           sw.Reset();
        //LINQ
        Console.WriteLine("\nLINQ Operations");

        sw.Start();
            var str1 = "1mir1112211a3bc9";
        for (int i = 1; i <= maxIterations; i++)
        {
            str1 = "1mir1112211a3bc9";
            str1 = String.Concat(str1.Where(c => c != '-' && (c < '0' || c > '9')) );
        }
        sw.Stop();

        Console.WriteLine("Finalstring: " + str1);
        Console.WriteLine("Elapsed time: " + sw.Elapsed.TotalMilliseconds + " Milliseconds");

        //Regex
        sw.Reset();
        
        Console.WriteLine("\nRegex Operations");

        sw.Start();
        var str3 = "1mir1112211a3bc9";
        for (int i = 1; i <= maxIterations; i++)
        {
            str3 = "1mir1112211a3bc9";
            str3 = Regex.Replace(str3, @"[\d-]", string.Empty);
        }
        sw.Stop();

        Console.WriteLine("Finalstring: " + str3);
        Console.WriteLine("Elapsed time: " + sw.Elapsed.TotalMilliseconds + " Milliseconds");

Here are the Results:

Removing digits from string: "1mir1112211a3bc9" with Total 100000x iterations

Replace Operations
Finalstring: mirabc
Elapsed time: 37,8307 Milliseconds

List Operations:
Finalstring: mirabc
Elapsed time: 16,7803 Milliseconds

LINQ Operations
Finalstring: mirabc
Elapsed time: 34,5803 Milliseconds

Regex Operations
Finalstring: mirabc
Elapsed time: 252,1907 Milliseconds

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM