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();
DisplayMatches(htmlfile);
Console.ReadKey();
}
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.
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
Rock the casbah !!!
ReplyDelete