Getting started with regex - Matching a valid date
If you haven't done so already, create a new console application and add a class to the project called RegExDemo
. Your code at this moment should look something like this:
class Program { static void Main(string[] args) { } } public class RegExDemo { }
Getting ready
For the purpose of this book, we are using a console application to illustrate the use of regex. In reality, you would probably not have this logic mixed in between your production code, because this would result in code being rewritten. The best place to add something such as regex is in a helper class within an extension method.
How to do it...
- In the console application, add the following
using
statement so that we can use the regex assembly in .NET:
using System.Text.RegularExpressions;
- 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...