c# regex replace string code example

Example 1: c sharp string replace

// You can use the function 'Replace()' to replace all instances
// of a string or character with a given string or character.
str  = "Hello World!"
str.Replace('o', 'e');
// Output: "Helle Werld!"

Example 2: c# regex get matched string

Regex filter = new Regex(@"(\d+)");

foreach (var item in checkedListBox1.CheckedItems)
{
    var match = filter.Match(item.ToString());
    if (match.Success)
    {
        MessageBox.Show(match.Value);
    }    
}

Example 3: c# regex replace

Regex.Replace(
  "input string", @"[a-zA-Z]+", "replace string"
);

Example 4: c# replace regex string

string pattern = @"\bwest\b";
string modifiedString = Regex.Replace(input, pattern, strReplacement, RegexOptions.IgnoreCase);