INTRODUCTION
A regular expression’s use is for finding substrings in a large string expression. It supports the use of various wild cards and other very useful techniques for performing the required actions. The namespace System.Text.RegularExpressions contains a number of classes associated with regular expressions.
USING REGULAR EXPRESSIONS
For this tutorial we need to define a quite large string of text. Copy the following code to your program, after making sure that you have added the System.Text.RegularExpressions namespace:
string inputText = @" This comprehensive compendium provides a broad and thorough investigation of all aspects of programming with ASP.Net. The asp programming language is a language devoted to web services. This book will give you the necessary information you need to maste the ASP.Net language";
This string will be searched for substrings with the use of regular expressions. It is actually a simple text search. To master regular expressions you should study heavy books and practice a lot. We define the string we want to search for. We can also choose if we want to ignore the letters’ cases or not:
string searchFor = "ASP";
Console.WriteLine(Regex.Match(inputText, searchFor, RegexOptions.IgnoreCase));
Console.ReadLine();
In the above example we define the string we want to searc for. Then, we use the Regex.Match method that finds and returns the first occurrence of the string that we search for. In the method’s parameters we have defined that we do not want to differentiate between different cases. The screen shows “asp”, the first three leters of the word “aspects”. If we change this option the console will now show “ASP”. This method returns the first occurrence of the specified string. We could also define a collection and use it to store a collection of identical objects for the matched results. Then, by iterating through the collection we can print all the occurences inside the input string. Use the followin code as a template:
static void Main(string[] args)
{
string inputText = @" This comprehensive compendium provides a broad and thorough investigation of all aspects of programming with ASP.Net. The asp programming language is a language devoted to web services. This book will give you the necessary information you need to master the ASP.Net language";
string searchFor = "ASP";
MatchCollection myMatches = (Regex.Matches(inputText, searchFor, RegexOptions.IgnoreCase));
foreach (Match nextMatch in myMatches)
{
Console.WriteLine(nextMatch.Value);
}
Console.ReadLine();
}
The resulted matches are stored in a collection named myMatches. We then use the objects in this collection and acquire their value property to display on the screen.
This was only an example to the use of regular expressions. Their real power results from the fact that the pattern string, the string we search for, doesn’t have to contain plain text. It can contain escape sequences or meta-characters. Meta-characters are special characters used for giving commands. For example you can specify a search string to search for any words beginning with a specified letter or beginning with a specified letter and ending with another one. You can also specify alternative characters (search for character a or character b).
Trackback(0)
 |