Introduction

A regular expression is a pattern that describes a string through the use of special characters that denote a specific bit of text to match. The use of regular expression is not a new concept in programming. For regex to work, it needs to use a regex engine that does all the heavy lifting.

In the .NET Framework, Microsoft has provided for the use of regex. To use regex, you will need to import the System.Text.RegularExpressions assembly to your project. This will allow the compiler to use your regex pattern and apply it to the specific text you need to match.

Secondly, regex have a specific set of metacharacters that hold special meaning to the regex engine. These characters are [ ], { }, ( ), *, +, , ?, |, $, ., and ^.

The use of the curly brackets { }, for example, enables developers to specify the number of times a specific set of characters need to occur. Using square brackets, on the other hand, defines exactly what needs to be matched.

If we, for example, specified [abc], the pattern would look for lowercase As, Bs, and Cs. regex, therefore, also allows you to define a range, for example, [a-c], which is interpreted in exactly the same way as the [abc] pattern.

Regular expressions then also allow you to define characters to exclude using the ^ character. Therefore, typing [^a-c] would find lowercase D through Z because the pattern is telling the regex engine to exclude lowercase As, Bs, and Cs.

Regular expressions also define d and D a type of shortcut for [0-9] and [^0-9], respectively. Therefore, d matches all numeric values, and D matches all non-numeric values. Another shortcut is w and W, which match any character from lowercase A to Z, irrespective of the case, all numeric values from 0 to 9, and the underscore character. Therefore, w is [a-zA-Z0-9_], while W is [^a-zA-Z0-9_].

The basics of regex are rather easy to understand, but there is a lot more that you can do with regex.

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

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