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: ...

Get C# 7 and .NET Core Cookbook now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.