Friday, September 17, 2010

using Regular Expressions in C# to find a phrase in a text and replace it with another

This is an example on how to find & replace a phrase in a string, the easy way in C# :

            
            using System.Text.RegularExpressions;
            ....
            if (Regex.IsMatch(content, searchText, System.Text.RegularExpressions.RegexOptions.IgnoreCase))
            {
                display.Text += "Found match in " + filePath +" ***\n";
                content = Regex.Replace(content, searchText, replaceText);
            }

No comments: