Getting started with regex

We will be create a new class in C# called Chapter9. Here, we will create various methods to illustrate the use of regex.

Getting ready

For the purpose of this book, we will create a simple console application to illustrate the use of regex. In reality, you would probably not have this logic mixed in with your production code, because this would result in code being rewritten. The best place to add something like regex is in a helper class within an extension method.

How to do it…

  1. Start by right-clicking the solution, going to Add, and then to New Project from the context menu:
    How to do it…
  2. The Add New Project window opens up. Select the Class Library project type and call the project Chapter9:
    How to do it…
  3. After the new class file has been added, your Solution Explorer should look like this:
    How to do it…
  4. Right-click the Class1.cs file and select Rename from the context menu:
    How to do it…
  5. Rename the Class1.cs file to Recipes.cs and select Yes from the confirmation dialog:
    How to do it…
  6. In the console application, click on the References section and select Add Reference from the context menu:
    How to do it…
  7. In Reference Manager for the console application, select Chapter9 and click on OK to add it as a reference to the console application:
    How to do it…
  8. In the Recipes class, add the following using statement so that we can use the regex assembly in .NET:
    using System.Text.RegularExpressions;
  9. After you have done all this, your Chapter9 class should look like this:
    using System.Text.RegularExpressions;
    namespace Chapter9
    {
        public class Recipes
        {
        }
    }

How it works…

We have added a basic class file that will be used to validate regex patterns, which is called from our console application.

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

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