TheDeveloperBlog.com


C# Regex.Replace Numbers

Regex.Replace numbers. Regex can match numeric characters. This helps with certain requirements. Some programs need to remove digit character to clean up input data. The "\d" metacharacter matches digit characters.

And: Conversely the "\D" metacharacter matches non-digit characters. Uppercase means "not."

Regex.Replace

Example. First, the requirement we have here is that the characters with the exact values '0' through '9' must be removed from the string. They are not replaced with other characters but entirely removed.

So: The string "6cylinder" will become the string "cylinder". The method works correctly on strings that have digits in any part.

C# program that removes numbers

using System;
using System.Text.RegularExpressions;

class Program
{
    /// <summary>
    /// Remove digits from string.
    /// </summary>
    public static string RemoveDigits(string key)
    {
	return Regex.Replace(key, @"\d", "");
    }

    static void Main()
    {
	string input1 = "Dot123Net456Perls";
	string input2 = "101Dalmatations";
	string input3 = "4 Score";

	string value1 = RemoveDigits(input1);
	string value2 = RemoveDigits(input2);
	string value3 = RemoveDigits(input3);

	Console.WriteLine(value1);
	Console.WriteLine(value2);
	Console.WriteLine(value3);
    }
}

Output

DotNetPerls
Dalmatations
 Score

The program defines two methods. RemoveDigits is used to remove the numeric characters from the input string. The Main method applies the RemoveDigits method on three input strings, and then writes the result to the screen.

Console.WriteLine

RemoveDigits is probably the simplest version of a correct method that removes only digits. The "\d" string specifies a single digit character ('0' through '9'). This code will not match negative numbers of numbers with decimal places.

Tip: The @ character designates a verbatim string literal that is escaped in a simpler way in the program.

Input and output. The program's result is that the three strings have their numbers removed. Internally, RemoveDigits receives a reference to the input strings, which would be interned in the runtime.

Then: The Regex allocates new string data on the managed heap and returns a reference to that object data.


Performance. The performance of this method is far from ideal. For a sensitive path in the control flow of your program, you would want to use a char array and dynamically build up the output string.

Finally: You could return that character array as a string using the new string constructor.

String Constructor

Discussion. There are many possible requirements when dealing with numbers embedded in strings. Several useful methods are available in the .NET Framework, such as char.IsDigit and int.TryParse.

char.IsDigitParse

Summary. You can remove number characters in strings using the C# language. This requirement is extremely common and the code here was taken from a release program developed by me. We proved the method's correctness on simple input.

Finally: We explored performance issues and other tasks related to numbers in strings.