Tag Archives: replace

Remove non-digits or non-letters from a string in C#

Sometimes you might want to extract only the digits, letters, or some other group of characters from a string. You could loop through the string examining each character individually. Fortunately there’s a much easier way to do this. The regular … Continue reading

Posted in parsing, regular expressions, strings | Tagged , , , , , , , , , , , , , , , | 2 Comments

Replace text in the lines of a string in C#

This example uses the following code to replace text in the lines of a string. private void btnGo_Click(object sender, EventArgs e) { try { Regex reg_exp = new Regex(txtPattern.Text); lblResult.Text = reg_exp.Replace( txtInput.Text, txtReplacementPattern.Text); } catch (Exception ex) { MessageBox.Show(ex.Message); … Continue reading

Posted in regular expressions, strings | Tagged , , , , , , , , , , , , | 6 Comments

Replace text matching a pattern in C#

This example uses the following code to replace text matching a pattern within a string. // Make the replacements. private void btnGo_Click(object sender, EventArgs e) { Regex reg_exp = new Regex(txtPattern.Text); lblResult.Text = reg_exp.Replace( txtTestString.Text, txtReplacementPattern.Text); } This code creates … Continue reading

Posted in regular expressions, strings | Tagged , , , , , , , , , , , | Leave a comment