Pattern Matching with Regular Expressions
Explore how to use regular expressions in C# to validate user input, split complex strings, and improve application performance. Understand common regex symbols and syntax while applying best practices in .NET development.
Regular expressions are useful for validating input from the user. They are very powerful and can get very complicated. Almost all programming languages support regular expressions and use a common set of special characters to define them. Let’s try out some example regular expressions:
Step 1: Use your preferred code editor to add a new Console App or console project named WorkingWithRegularExpressions to the Chapter08 solution or workspace:
In Visual Studio Code, select
WorkingWithRegularExpressionsas the activeOmniSharpproject.
Step 2: In Program.cs, delete the existing statements, and then import the following namespace:
Checking for digits entered as text
We will start by implementing the common example of validating number input:
Step 1: In Program.cs, add statements to prompt the user to enter their age and then check that it is valid using a regular expression that looks ...