Matching a valid date
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.
Getting ready
Ensure that you have added the correct assembly to your class. At the top of your code file, add the following line of code if you haven't already done so:
using System.Text.RegularExpressions;
How to do it…
- 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) { }
- Add the following regex pattern to your method, ...
Get C# Programming 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.