To use a regular expression on a file you must first read in the file into a string. Here's a console program that opens a StreamReader on the file and reads in each line.
using System;
using System.IO;
using System.Text.RegularExpressions;
class Program
{
static void Main()
{
Regex regex = new Regex(@
"\s/Content/([a-zA-Z0-9\-]+?)\.aspx");
// "\s/Content/" : space and then Content directory
// "([a-zA-Z0-9\-]+?) : group of alphanumeric characters and hyphen
// ? : don't be greedy, match lazily
// \.aspx : file extension required for match
using (StreamReader reader = new StreamReader(@
"C:\programs\log.txt"))
{
string line;
while ((line = reader.ReadLine()) != null)
{
// Try to match each line against the Regex.
Match match = regex.Match(line);
if (match.Success)
{
// Write original line and the value.
string v = match.Groups[1].Value;
Console.WriteLine(line);
Console.WriteLine(
"... " + v);
}
}
}
}
}
2008-10-16 23:56:44 W3SVC2915713 GET /Content/String.aspx - 80 66.249
2008-10-16 23:59:50 W3SVC2915713 GET /Content/Trim-String-Regex.aspx - 80 66.249
2008-10-16 23:56:44 W3SVC2915713 GET /Content/String.aspx - 80 66.249
... String
2008-10-16 23:59:50 W3SVC2915713 GET /Content/Trim-String-Regex.aspx - 80 66.249
... Trim-String-Regex