C# Code Snippet - Extract URLs

C# Code Snippet - Extract URLs

C# Code Snippet - Extract URLs

(C-Sharp) C# code snippet to extracts all the URLs from a string. ExtractURLs returns string array of URLs successful matches by iteratively applying a regular expression pattern to the input string.

Bookmark:

C# Code Snippet - Extract URLs

This .Net C# code snippet extracts all the URLs from a string. Data mining for URLs done by set of successful matches found by iteratively applying a regular expression pattern to the input string.

public string[] ExtractURLs(string str)
{
    // match.Groups["name"].Value - URL Name
    // match.Groups["url"].Value - URI
    string RegexPattern = @"<a.*?href=[""'](?<url>.*?)[""'].*?>(?<name>.*?)</a>"

    // Find matches.
    System.Text.RegularExpressions.MatchCollection matches
        = System.Text.RegularExpressions.Regex.Matches(str, RegexPattern, System.Text.RegularExpressions.RegexOptions.IgnoreCase);

    string[] MatchList = new string[matches.Count];

    // Report on each match.
    int c = 0;
    foreach (System.Text.RegularExpressions.Match match in matches)
    {
        MatchList[c] = match.Groups["url"].Value;
        c++;
    }

    return MatchList;
}


C# Keywords Used:

  • Regex
  • Match
  • MatchCollection
  • RegexOptions.IgnoreCase
  • Regex.Matches

Code Snippet Information:

  • Applies To: .Net, C#, CLI, Data Mining, URL Extract, Regular Expression
  • Programming Language : C#

External Resources:

Chris Buckler :: February 24-2011 :: 10:21 AM

This does not even work...

Chris Buckler :: February 24-2011 :: 10:22 AM

Not even after fixing the syntax errors...

Leave a comment