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;
2
  • 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? Commented Mar 21, 2014 at 17:39
  • 1
    Don't use Regex to parse HTML Consider something like HtmlAgilityPack Commented Mar 21, 2014 at 17:46

2 Answers 2

2

It should be:

 htmlcode1 = match.Groups["link"].Value;
Sign up to request clarification or add additional context in comments.

Comments

0

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

htmlcode1.Contains("Links")

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.