Gold
Gold

Reputation: 62554

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

Upvotes: 102

Views: 184185

Answers (9)

Chris Raisin
Chris Raisin

Reputation: 442

After seeing how much faster a list operation is in replacements, I created a user Class (in VB.Net) named "CKRReplace". The "CKR" prepend is my initials, rather than calling it simply "Replace" (which is used so often elsewhere in VB Code).

    Public Class CKRReplace
      Public Function AllNumbers(strSource As String, _
                                 Optional strReplace As String = "") _
                                 As String
        strSource = strSource.Replace("1", strReplace) _
        .Replace("2", strReplace) _
        .Replace("3", strReplace) _
        .Replace("4", strReplace) _
        .Replace("5", strReplace) _
        .Replace("6", strReplace) _
        .Replace("7", strReplace) _
        .Replace("8", strReplace) _
        .Replace("9", strReplace) _
        .Replace("0", strReplace)
        Return strSource
    End Function
  End Class

Calls to the code to remove all numbers would be from the string "1mir1112211a3bc9" would be something like (again in VB.Net): Dim oReplace as new CKRReplace Dim str1 as string = "1mir1112211a3bc9" oReplace.AllNumbers(str1,"")

The second parameter passed to the oReplace object is not really required since that parameter defaults to "", but I included it for clarity.

Upvotes: 0

amehmood
amehmood

Reputation: 61

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

Upvotes: 4

Shubham Smiley
Shubham Smiley

Reputation: 21

text= re.sub('[0-9\n]',' ',text)

install regex in python which is re then do the following code.

Upvotes: 2

Ali Rasoulian
Ali Rasoulian

Reputation: 439

Blow codes could help you...

Fetch Numbers:

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

Fetch Letters:

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

Upvotes: 13

Vitaly Yakel
Vitaly Yakel

Reputation: 21

the best design is:

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

Upvotes: 2

Sgedda
Sgedda

Reputation: 1541

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 "

Upvotes: 3

Guffa
Guffa

Reputation: 700730

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.

Upvotes: 29

Darin Dimitrov
Darin Dimitrov

Reputation: 1039408

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

Upvotes: 5

Noldorin
Noldorin

Reputation: 147461

Try the following:

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

The \d identifier simply matches any digit character.

Upvotes: 169

Related Questions