Search this blog

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

Search the blog

Loading

Thursday, September 2, 2010

How to use back references to match the patterns?

Hi,
I have provided a sample example of how to retrieve matched data from a text file using backreferences. This tutorial will help us in understanding how to create a regular expression for matching the repeating patterns from a source.I will explain in the video tutorial about how to match repeating letters and how to match repeating words from a given source. We will also see how to construct a regular expression for the above scenarios.After we do that we will see how to extract matched data from the source and use it for our purpose.

Video tutorial part - 1



Video Tutorial Part - 2




using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Configuration;
using System.IO;
using System.Threading;
using System.Security.Permissions;
using System.Text.RegularExpressions;
namespace Example1
{
    class Program
    {

        static void Main(string[] args)
        {
            string SampleText = CreateString();
            DisplayMatches(SampleText);
            Console.ReadKey();
          
        }

        static string CreateString()
        {
            FileStream fs = new FileStream(@"C:\My stuff\Work Stuff\Blog\Test\SampleText.txt", FileMode.Open);

            StreamReader sr = new StreamReader(fs);
          
            string s = sr.ReadToEnd();

            sr.Close();
            fs.Close();
            return s;

          

        }


        static void DisplayMatches(string s)
        {
          
            Regex r;
            Match m;




      
            for (m = r.Match(s); m.Success; m = m.NextMatch())
            {
                Console.WriteLine("Found repeating words  at "
                + m.Groups[1].Index + " And the macth is " + m.Groups[1].Value);
            }
        }
    




    }
}

Regards
Sameer Shaik

Tags: Microsoft MCTS exam, Learn, Tutorials, Free C#, Free Videos, Microsoft Dot Net Jobs,Microsoft Dot Net Jobs Interview, Free Training and Preparation, Jobs MCTS specialization, visual studio.net, study guide, sample questions, Exam Prep, Exam Practice Test

No comments:

Post a Comment