Tag Archives: pattern matching

Find files that match multiple patterns in C#

The System.IO.Directory.GetFiles method lets you easily search for files in a directory that match a pattern. Unfortunately it can only search for files that match a single pattern. For example, if you want to find files that match the patterns … Continue reading

Posted in .NET, files | Tagged , , , , , , , , , , , , , , | Leave a comment

Understand C# regular expression symbols

The following table shows useful regular expression symbols, commands, and other items that you can use in regular expressions in C# programs. Item Purpose \ Either begins a special symbol such as \n or escapes the following character ^ Matches … Continue reading

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

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

Show regular expression matches in C#

When you click the Go button, the program uses the following code to display regular expression matches in a string. Regex reg_exp = new Regex(txtPattern.Text); MatchCollection matches = reg_exp.Matches(txtTestString.Text); rchResults.Text = txtTestString.Text; foreach (Match a_match in matches) { rchResults.Select(a_match.Index, a_match.Length); … Continue reading

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

Use regular expressions to rename files within a date range and that match a pattern in C#

This example extends the example Use regular expressions to rename files that match a pattern in C# to let you rename files within a date range. See the previous example for most of the details. This post describes the changes … Continue reading

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

Use regular expressions to rename files that match a pattern in C#

This example shows how to use regular expressions to rename files. To use the program, enter a directory name and a file pattern. Also enter regular expression find and replace patterns. When you click Preview Changes, the program searches the … Continue reading

Posted in files, regular expressions | Tagged , , , , , , , , , , | 4 Comments