How to do it...

  1. In the console application, add the following using statement so that we can use the regex assembly in .NET:
        using System.Text.RegularExpressions;
  1. We will create a regex to validate a date pattern of yyyy-mm-dd, yyyy/mm/dd, or yyyy.mm.dd. At first, the regex will look daunting, but bear with me. When you have completed the code and run the application, we will dissect the regex. Hopefully, the expression logic will become clear.
  1. Inside the RegExDemo class, create a new method called ValidDate() that takes a string as the parameter. This string will be the date pattern we want to validate:
        public void ValidDate(string stringToMatch) 
{

}
  1. Add the following regex pattern to your method to a variable in the method:
        string pattern = $@"^(19|20)dd[-./](0[1-9]|1[0-2])
[-./](0[1-9]|[12][0-9]|3[01])$";
  1. Finally, add the regex to match the supplied string parameter:
        if (Regex.IsMatch(stringToMatch, pattern)) 
Console.WriteLine($"The string {stringToMatch}
contains a valid date.");
else
Console.WriteLine($"The string {stringToMatch} DOES
NOT contain a valid date.");
  1. When you have done this, your method should look like this:
        public void ValidDate(string stringToMatch) 
{
string pattern = $@"^(19|20)dd[-./](0[1-9]|1[0-2])
[-./](0[1-9]|[12][0-9]|3[01])$";

if (Regex.IsMatch(stringToMatch, pattern))
Console.WriteLine($"The string {stringToMatch} contains
a valid date.");
else
Console.WriteLine($"The string {stringToMatch} DOES
NOT contain a valid date.");
}
  1. Going back to your console application, add the following code and debug your application by clicking on Start:
        RegExDemo oRecipe = new RegExDemo(); 
oRecipe.ValidDate("1912-12-31");
oRecipe.ValidDate("2018-01-01");
oRecipe.ValidDate("1800-01-21");
oRecipe.ValidDate($"{DateTime.Now.Year}
.{DateTime.Now.Month}.{DateTime.Now.Day}");
oRecipe.ValidDate("2016-21-12");
Console.Read();
You will notice that if you add the using static System.Console; namespace, you then just need to call Read() instead of Console.Read(). This new feature where you could import static namespaces was added in C# 6.0.
  1. The date strings are passed to the regex, and the pattern is matched against the date string in the parameter. The output is displayed in the console application:
  1. If you look at the output carefully, you will notice that there is a mistake. We are validating the date string in the format yyyy-mm-dd, yyyy/mm/dd, and yyyy.mm.dd. If we use this logic, our regex has incorrectly flagged a valid date as invalid. This is the date 2016.4.10, which is April 10, 2016, and is in fact quite valid.
We will explain shortly why the date 1800-01-21 is invalid.
  1. Go back to your ValidDate() method and change the regular expression to read as follows:
        string pattern = $@"^(19|20)dd[-./](0[1-9]|1[0-2]|[1-9])
[-./](0[1-9]|[12][0-9]|3[01])$";
  1. Run the console application again and look at the output:

This time the regex worked for all the given date strings. But what exactly did we do? This is how it works.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.137.223.10