Search this blog

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

Search the blog


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();
            Console.WriteLine("We will now replace the dates with our custom format to the file");
            string ChangedText = ReplaceString(SampleText);

        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();

            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);


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, study guide, sample questions, Exam Prep, Exam Practice Test

No comments:

Post a Comment