Today, in this tutorial, we are going to learn about c# regular expression programming and its practical implementation. Almost all programming languages have support for a regular expression and thus have a regular expression engine which parses the expressions, computes necessary and returns expected results. .NET manages this regular expression engine globally and thus works exactly the same way for all languages inside the .NET framework. If you are using a different language. NET(j#,VB, etc) other than c#, you can implement this in a similar way, just by changing the syntax to that language.
Don’t you know anything about Regular Expression at all? So, it is best to get some basic idea of regular expression before you start implementing it in C#. Go ahead and learn some regular expression basics and know what the symbols stands for.
RegularExpressions Namespace in .NET Framework:
First, we will need to import the System.Text.RegularExpressions namespace so that the regular expression classes are available. There are several classes for regular expression operation purpose like Group,Match,Capture and their corresponding Collection classes, one delegate ‘MatchEvaluator’, Regex(Main class to be used), Enum ‘RegexOptions’ and RegexCompilationInfo class, used for using Regular expression on stand alone assembly.
Instead of Regex, Match and RegexOptions classes, other classes are for a little advanced usage. So, I am not going to cover them in this article. Match class store a single match result, and MatchCollection stores a list of match results. RegexOptions is an enum which has various options to pass as parameters while calling match functions like Ingore case(accepts both lower and higher case letters), RightToLeft(Matches On reverse order then defaults left to right matching) and so on.
Using C# Regular Expression ‘Regex’ class:
As I already mentioned, Regex is the main class that does the real match operation. You can use this class either by instantiating an object or by calling it its static method. If you have such a situation where the same regular expression will be needed several times in a single execution, then it is better to use an instantiated object and check that against different values. Here are sample codes for such usage:
string[] email_lists = { "email1@domain1.com", "email2@domain1.com", "email1@domain2.com" };
Regex regx = new Regex("[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,6}$", RegexOptions.IgnoreCase);
foreach (string email in email_lists)
{
Match match = regx.Match(email);
Console.WriteLine(match.Success);
}
Code language: PHP (php)
The above example will validate a list of email addresses whether they are in correct email format and will print true/false depending on the match result. Note that, it matches every single string separately and returns a ‘Match’ class instance, which has a ‘Success’ property that indicates whether matches are successful or failed.
Alternative Static Match function:
If we use the static ‘Match’ method of the Regex class, we will have to feed two parameters at least(+1, optional RegexOptions). The following code sample will illustrate this usage:
string[] email_lists = { "email1domain1.com", "email2@domain1.com", "email1@domain2.com" };
string pattern = "[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,6}$";
foreach (string email in email_lists)
{
Match match = Regex.Match(email, pattern ,RegexOptions.IgnoreCase);
Console.WriteLine(match.Success);
}
Code language: PHP (php)
The above code snippet will work similarly as the previous one, just we have change the way of usage. But final result is same.
Retrieve all matched results from A Text:
OK, so as we know can match to a specific data, lets move one step forward to retrieves a list of data items which matches a specific regular expression. The following code snippet will do this work and retrieve all data matching simple time format hh:mm:ss .
Regex dataPointRegex = new Regex(@"\d[\d+.:]+\d", RegexOptions.IgnorePatternWhitespace);
String longText = "03:08:12asdasd06:47:12asdasd03:08:12asdasd06:47:42asdasdasdasd03:08:12ghjklfghdgodjg06:48:12asdada03:08:12asdasdasd06:48:42asdasdasdasdas";
List<String> segments = new List<String>();
MatchCollection matches;
matches = dataPointRegex.Matches(longText );
if (matches.Count > 0)
{
foreach (Match match in matches)
{
if (match.Success)
{
foreach (Group result in match.Groups)
{
segments.Add(result.Value);
}
}
}
}
Code language: PHP (php)
Note that, this example usage ‘Matches’ function instead of ‘Match’ to catch multiple matches. And each match object has its own associated ‘Groups’ collection that contains the results.
References:
Microsoft Product, VisualStudio.NET, has a very rich online MSDN documentation for full references on the .NET framework. To know more details on the regular expression tutorial in c#, you can go through the .NET Regular Expressions On MSDN Online. There, you will also find some handy examples. Also, let me know if you are having trouble with any specific portion which you haven’t found yet. Happy coding 🙂
Kuldeep Bansal says
Really this is a good article, which helps a lot for beginners as me as well as developer. Check this link….
http://mindstick.com/Blog/100/Regular%20Expressions%20in%20C
also helpful.