Deleting characters matching a pattern

Deleting characters matching a pattern

How to:

Want to ditch some characters? Here’s how in C#:

using System;
using System.Text.RegularExpressions;

class PatternDeletion
    static void Main()
        string originalText = "B4n4n4 P1zza!";
        string pattern = @"[0-9]+"; // Remove all digits
        string cleanedText = Regex.Replace(originalText, pattern, string.Empty);
        Console.WriteLine(cleanedText); // Outputs: Bnnn Pzza!

Need to snip ‘a’ followed by a digit? Behold:

string targetedRemoval = "C4ndy C4ne";
string complexPattern = @"a[0-9]"; // Targets 'a' followed by any digit

string refinedText = Regex.Replace(targetedRemoval, complexPattern, string.Empty);

Console.WriteLine(refinedText); // Outputs: Cndy Cne

Deep Dive

Regex (Regular Expressions) powers pattern-matching feats, going back to theoretical roots in the 1950s (thanks, automata theory!). Alternatives to regex include straight String.Replace() for simpler replacements, or custom algorithms if performance is critical (because regex has some overhead). These alternatives lack the flexibility and precision which makes regex a go-to for complex patterns. Implementing pattern deletion, be mindful of regex’s double-edged sword nature – they’re powerful but can be cryptic and slow for extensive data.

See Also