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

No comments:

Post a Comment