0

I am working on c# and am grabbing only links (an example) to the downloaded html code.

I know that I have the code of the website in the string htmlcode.

However I can't seem to get this to allow me to put match into a string. Below is my code:

 public string getURL()
    {
        /* Web client being opened up and being ready to read */
        WebClient webclient = new WebClient();
        Uri URL = new Uri("http://www.pinkbike.com");
        string htmlcode = webclient.DownloadString(URL);
        /* Time to grab only the links */
        string pattern = @"a href=""(?<link>.+?)""";
        Regex regex = new Regex(pattern, RegexOptions.IgnoreCase);
        MatchCollection MC = regex.Matches(htmlcode);
        string htmlcode1;
        foreach(Match match in MC)
        {
            /* Error location */
            htmlcode1 = match.Groups["link"];

        }
        return htmlcode1;
Chris Ballard
  • 3,771
  • 4
  • 28
  • 40
Bryce
  • 447
  • 2
  • 9
  • 24
  • Have you looked at what the type of `match.Groups["link"]` is? Does the compiler not tell you the type that you are trying to convert to string thus making it easy to look up in documentation? – Chris Mar 21 '14 at 17:39
  • 1
    [Don't use Regex to parse HTML](http://stackoverflow.com/questions/1732348/regex-match-open-tags-except-xhtml-self-contained-tags) Consider something like [HtmlAgilityPack](http://htmlagilitypack.codeplex.com/) – Bryan Crosby Mar 21 '14 at 17:46

2 Answers2

2

It should be:

 htmlcode1 = match.Groups["link"].Value;
pquest
  • 3,151
  • 3
  • 27
  • 40
0

You can also you String function Contains(). instated of using regex

htmlcode1.Contains("Links")

Avinash patil
  • 1,689
  • 4
  • 17
  • 39