Search this blog

You can search for all the topics in our blog using our google custom search now.

Search the blog

Loading

Wednesday, June 23, 2010

Learning regular expression step by step tutorials: Step 1

What is a regular expression?
It is a set of characters that can be compared to a string to determine whether the string meets specified format requirements.

The most basic form of regular expression is using Literal Characters. For ex: "t" will match any first occurrence in "mcts tutorials blog", which is simply "t" in "mcts", if you notice there is a second match too, but we haven't developed regular expression to evaluate any occurrence after the first.

Create a new console application and double click on program.cs file. Add the namespace System.Text.RegularExpressions.Regex.IsMatch() is the method which is going to evaluate your regular expression i.e. It is the going to compare the given string with the regular expression. If the match is successful then it is going to return true or mismatch results in false.






Your code should look like this:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;

namespace RegexStep1
{
    class Program
    {
        static void Main(string[] args)
        {
            string s = "Literal Characters";
            string regex = "tr";
            if (Regex.IsMatch(s, regex))
            {
                Console.WriteLine("Given string " + s + " matches with regular expression " + regex);
                Console.ReadKey();
            }
            else
            {
                Console.WriteLine("Given string " + s + " does not match with regular expression " + regex);
                Console.ReadKey();

            }
          
        }
    }
}


Please check out the video for detailed explanation on this example.

1 comment:

  1. I am glad I read this. Your information is thought-provoking, interesting and well-written. Thank you for sharing your knowledge.

    www.n8fan.net

    ReplyDelete