Category Archives: regular expressions

Move a window with a title that matches a pattern in C#

The example Set another application’s size and position in C# uses the FindWindow API function to find a window with a given title. Unfortunately FindWindow only finds the window if the title is an exact match. If you only know … Continue reading

Posted in API, miscellany, regular expressions, system | Tagged , , , , , , , , , , , , | Leave a comment

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 , , , , , , , , , , , , , , , | Leave a comment

List unique words in a text file in C#

This example uses regular expressions and LINQ to list the unique words contained in a text file in C#. When you enter the name of a file and click List Words, the following code executes. // List the words in … Continue reading

Posted in files, LINQ, regular expressions | Tagged , , , , , , , , , , | 1 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 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

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

Make a string extension to determine whether a string matches a regular expression in C#

Regular expressions let you (relatively) easily determine whether a string matches some sort of pattern. This example shows how to make a string extension method to determine whether a string matches a regular expression in C# The following StringExtensions class … Continue reading

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