Regular Expression in C#

A regular expression is a special text string for describing a search pattern. We use it almost daily for example when we search only text files from file system by using *.txt. By this we mean show me the files which have .txt at the end. It offers us search, find match, replacement etc from huge document. Some rules we need to remember.

^	Start of a string.
$	End of a string.
.	Any character (except \n newline)
|	Alternation.
{...}	Explicit quantifier notation.
[...]	Explicit set of characters to match.
(...)	Logical grouping of part of an expression.
*	0 or more of previous expression.
+	1 or more of previous expression.
?	0 or 1 of previous expression; also forces minimal matching when an expression might 
        match several strings within a search string.
\	Preceding one of the above, it makes it a literal instead of a special character. Preceding special matching character. For example we want to create a pattern where '(' will be treated as individual character than parenthesis. In that case we use \(, this is will be as '(' character.
\d    Represent  any number
\D    Represent  anything but not a number
\s    Represent a space
\S    Represent anything other than a space
\w   Any character 
\W   Anything  that is not a character
.      Present any character (not actual . dot) except  new line
\.    Present  actual .(dot)
\b   Present  space between word
a\(c\)     the pattern is  a(c), ( is treated as char not parenthesis
^(\(\d{3}\)|^\d{3}[.-]?)?\d{3}[.-]?\d{4}$  (788)123-1234  or  788.123-1234 should match
[a-z]+\.   useful.
[a-z]*\.    useful.
[a-z]?\.    useful. (only last l. matches)

Let's try to digest these by using in C# little by little. First example shows how to use ^.

class Program
    {
        static void Main(string[] args)
        {
            string pattern = "^ba";
            string myString = "baabyba ";
            bool match = Regex.IsMatch(myString, pattern);
            Console.WriteLine(match); // print True because there is ^ symbol and ba

            string pattern2 = "^ba";
            string myString2 = "bdabyba ";
            bool match2 = Regex.IsMatch(myString2, pattern2);
            Console.WriteLine(match2); // print False, there is no ba at the beginning

            string pattern3 = "ba";
            string myString3 = "bdabyba ";
            bool match3 = Regex.IsMatch(myString3, pattern3);
            Console.WriteLine(match3); // print True

            string pattern4 = "ba";
            string myString4 = "bdabybl";
            bool match4 = Regex.IsMatch(myString4, pattern4);
            Console.WriteLine(match4); // print False
            Console.ReadKey();
        }
    }

In the previous code example, we see output True, False, True and False. Now lets use rest of those.

    //Use of $
    class Program
    {
        static void Main(string[] args)
        {
            string pattern = "ba$";
            string myString = "baabyba";
            bool match = Regex.IsMatch(myString, pattern);
            Console.WriteLine(match); // print True because there is $ at the end

            string pattern2 = "ba$";
            string myString2 = "bdabyba ";
            bool match2 = Regex.IsMatch(myString2, pattern2);
            Console.WriteLine(match2); // print False, space at end

            string pattern5 = "^ba*.*ba$";  // ^ba$ does not work
            string myString5 = "baMahbubba";
            bool match5 = Regex.IsMatch(myString5, pattern5);
            Console.WriteLine(match5); // print True

            Console.ReadKey();
        }
    }

Use of . | and {}

    class Program
    {
        static void Main(string[] args)
        {
            string pattern = "^.";
            string myString = "baabyba";
            bool match = Regex.IsMatch(myString, pattern);
            Console.WriteLine(match); // print True because . means any char.

            string pattern1 = "ab|cd";
            string myString1 = "abc fuel station in cd wing";
            bool match1 = Regex.IsMatch(myString1, pattern1);
            Console.WriteLine(match1); // print True because there are both ab anc cd 

            string pattern2 = "a{3,4}"; // char a can be 3 to 4 times
            string myString2 = "I write 4 (aaaa) and 2 aa";
            bool match2 = Regex.IsMatch(myString2, pattern2);
            Console.WriteLine(match2); // print True because of aaaa not for aa

            Console.ReadKey();
        }
    }

//Use of \d {} \s and ()

 
    class Program
    {
        static void Main(string[] args)
        {
            string pattern = @"\d{2,4}\s(ab)"; // digit 2 to 4, space and ab as group
            string myString = "I write 4444abcabc123 ab";
            bool match = Regex.IsMatch(myString, pattern);
            Console.WriteLine(match); // print True because . means any char.
            Console.ReadKey();
        }
    }