102

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

Example input: 123- abcd33
Example output: abcd

1
  • 3
    you also want the "- " removed? Those are not numbers... Commented Nov 1, 2009 at 14:23

9 Answers 9

170

Try the following:

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

The \d identifier simply matches any digit character.

Sign up to request clarification or add additional context in comments.

2 Comments

What is the purpose of the dash? I know it's used for ranges, but there's nothing at the right?
Purpose is to remove it together with numbers. See input/output example in question.
29

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.

5 Comments

@SirDemon: Yes, LINQ is usually not the fastest option, but regular expressions have a bigger initial overhead. For operations on short strings setting up the RegEx object takes longer than the actual work.
@Guffa Do you know how this scales? Lets say on 50k records should I go for RegEx?
@ArnoldWiersma: Either should scale pretty well, they are both basically linear, so there are no nasty surprises. I can't tell off hand which would be faster, you would have to test that.
alternatively new string(text.Where(char.IsLetter).ToArray());
@SkorunkaFrantišek, the problem here is that this also trims the string
13

Blow codes could help you...

Fetch Numbers:

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

Fetch Letters:

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

Comments

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

Comments

4

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

2 Comments

Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.
I liked seeing the comparison between the methods. I will still use the Regex approach for readability and because I only have a few iterations to do, but it is still good to know how much slower the Regex is compared to the other methods.
3

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 "

Comments

2

the best design is:

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

Comments

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

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

Comments

0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.