0

I'm trying to take html code from a page on the internet, save it as a text file, then read the text file find a part of the code, save it as a var and out put it to the console in c#.

this is the code i'm trying but it doesn't work

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApplication3
{
    class StringSearch
    {
        static void Main()
        {
            string HTML = System.IO.File.ReadAllText(@"C:\Users\gamer\Desktop\HTML\code test.txt");
        string sPattern = "code";

        foreach (string s in HTML)
        {
            System.Console.Write("{0,24}", s);

            if (System.Text.RegularExpressions.Regex.IsMatch(s, sPattern, System.Text.RegularExpressions.RegexOptions.IgnoreCase))
            {
                System.Console.WriteLine("  (match for '{0}' found)", sPattern);
            }
            else
            {
                System.Console.WriteLine();
            }
        }

        System.Console.WriteLine("Press any key to exit.");
        System.Console.ReadKey();

        }
    }
}

P.S if you know a way to capture a pages HTML code/part of a pages HTML code and out put it that would be even better Thanks

Uwe Keim
  • 39,551
  • 56
  • 175
  • 291
  • `foreach (string s in HTML)` won't work, because `HTML` is a *string*. If you wanted to read lines, you should use `File.ReadAllLines` or `File.ReadLines`. – Jon Skeet Jul 05 '16 at 07:01
  • Let's check HtmlAgilityPack. Even if parsing some well-defined subset of HTML is possible with RegEx (and [more is possible](http://stackoverflow.com/a/4234491/1207195) with an huge amount of effort and deep knowledge of regexes) [you'd better](http://stackoverflow.com/a/1732454/1207195) use a parser, easier and faster. – Adriano Repetti Jul 05 '16 at 07:02
  • Why would you need to save the file to disk? you could just split it depending on whatever criteria you need to find the code while it's in memory. since it's appearantly html you're looking through, i don't think the file would be big enough to merit saving the file to disk, then streaming through it. – Timothy Groote Jul 05 '16 at 07:02

0 Answers0