Search this blog

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

Search the blog


Tuesday, August 24, 2010

Extract matched data using regular expressions C# example

This example will help you understand how to extract matched data using regular expressions and display it on the console.
Watch this tutorial first on using BackReferences:

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

namespace Regex1
    class Program
        static void Main(string[] args)
            string htmlfile = CreateString();

        static string CreateString()
            FileStream fs = new FileStream(@"C:\Users\Ship\Documents\Visual Studio 2008\Projects\Regex1\Regex1\Sample.aspx", FileMode.Open);

            StreamReader sr = new StreamReader(fs);

            string s = sr.ReadToEnd();

            return s;


        static void DisplayMatches(string s)
            Regex r;
            Match m;
            r = new Regex(@"href\s*=\s*(?:""(?<1>[^""]*)""|(?<1>\S+))", RegexOptions.IgnoreCase|RegexOptions.Compiled);
            for (m = r.Match(s); m.Success; m = m.NextMatch())
                Console.WriteLine("Found href " + m.Groups[1] + " at "
                + m.Groups[1].Index);

Video tutorial for this post will be uploaded soon.

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

1 comment: