Friday, November 30, 2007

Using Regular Expressions in C#

Regular Expressions are a powerful pattern matching language. Regular Expressions allow you to apply a pattern to an input string and return a list of the matches within the text. Regular expressions also allow text to be replaced using replacement patterns.

Below is simple example of usage in search and replace context

Regex rexSearchPattern = new Regex("Sechin",RegexOptions.IgnoreCase);
sBatchFileContents =
rexSearchPattern.Replace(sBatchFileContents,Sachin);

Above I am searching for 'Sechin' in sBatchFileContents and replacing it with 'Sachin' and this Expression is Case insensitive so it will also replace sechin.