Search this blog

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

Search the blog

Loading

Friday, September 3, 2010

How to extract matched data from a source and replace substrings using regular expressions

In this tutorial we will detect a specific pattern from given source file and edit the source file automatically with our custom format substring pattern

Video Part - 1



Video 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.WriteLine("We will now replace the dates with our custom format to the file");
            string ChangedText = ReplaceString(SampleText);
            WriteToFile(ChangedText);
            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;
r = new
              
            for (m = r.Match(s); m.Success; m = m.NextMatch())
            {
                Console.WriteLine("Found a date in the source"
                + " "+ m.Result("${day}/${month}/${year}"));
            }
        }

        static string ReplaceString(string s)
        {

         
       
        }

        static void WriteToFile(string s)
        {
            FileStream fs = new FileStream(@"C:\My stuff\Work Stuff\Blog\Test\SampleText.txt", FileMode.Open,FileAccess.Write);
            StreamWriter sw = new StreamWriter(fs);
            sw.Write(s);
            sw.Close();
            fs.Close();
            
        }
      




    }
}

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

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