Regex has been the most popular and easiest way of writing validations. The only big problem with regex has been the cryptic syntax. Developers who are working on projects with complicated validation always refer some kind of cheat sheet to remember the syntaxes and commands.

In this article we will try to understand what regex is and how to remember those cryptic syntaxes easily.

FYI :- This article will use c# language to demonstrate regex implementation , so in case you are using other languages , syntaxes can change accordingly.

The best way to remember regex syntax is by remembering three things Bracket, caret and Dollars.

B

There are 3 types of brackets used in regular expression

Square brackets “[“and Curly “{“ brackets.

Square brackets specify the character which needs to be matched while curly brackets specify how many characters. “(“ for grouping.

We will understand the same as we move ahead in this article.

C

caret “^” marks the start of a pattern.^ may appear at the beginning of a pattern to require the match to occur at the very beginning of a line. For example, ^xyz matches xyz123 but not 123xyz.

D

Dollar “$” marks the end of a pattern.$ may appear at the end of a pattern to require the match to occur at the very end of a line. For example, pqr$ matches 123pqr but not pqr123.

Caret (^) and dollar sign ($) indicate the pattern to the beginning or end of the string being searched.The two anchors may be combined. For example, ^pqr$ matches only pqr. Any characters after or before it will make the pattern invalid.

Now once you know the above three syntaxes you are ready to write any validation in the world. For instance the below validation shows how the above three entities fit together.

The above regex pattern will only take characters which lies between ‘a’ to ‘z’. The same is marked with square bracket to define the range.

The curly bracket's indicates the minimum and maximum length.

Finally caret sign at the start of regex pattern and dollar at the end of regex pattern specifies the start and end of the pattern to make the validation more rigid.

So now using the above 3 commands let’s implement some regex validation.

In the previous question the regex validator will only validate first 3 characters of the invoice number if it is in small letters. If you put capital letters it will show as invalid. To ensure that the first 3 letters are case insensitive we need to use ^[a-zA-Z]{3} for character validation.

The time taken to evaluate a regex expression is directly proportional to the complexity and the number of characters of the regular expression. In simple words if you have many characters it would take more time for parsing.

This typical behavior of regex parsing can be exploited by hackers to hang your site forever by making a DOS attack. Below is a nice facebook .NET regex video which demonstrates how regex DOS attack looks like.

To overcome this problem in .NET 4.5 they have introduced regex timeout feature, you can read about the same from here