Advertisement
Razhagal

Extract URLs

Mar 31st, 2014
427
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.02 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Text.RegularExpressions;
  6. using System.Threading.Tasks;
  7.  
  8. class ExtractURL
  9. {
  10.  
  11.     static List<string> GetLinks(string text)
  12.     {
  13.         List<string> links = new List<string>();
  14.  
  15.         Regex urls = new Regex(@"((https?|ftp|file)\://|www.)[A-Za-z0-9\.\-]+(/[A-Za-z0-9\?\&\=;\+!'\(\)\*\-\._~%]*)*",
  16.                                RegexOptions.IgnoreCase);
  17.  
  18.         MatchCollection matches = urls.Matches(text);
  19.         foreach (Match match in matches)
  20.         {
  21.             links.Add(match.Value);
  22.         }
  23.  
  24.         return links;
  25.     }
  26.     static void Main()
  27.     {
  28.         string someText = Console.ReadLine();
  29.  
  30.         List<string> urls = GetLinks(someText).Distinct().ToList(); //Again use Distinct if you dont want repeated elements
  31.  
  32.         Console.WriteLine();
  33.         Console.WriteLine("The url links are:");
  34.         Console.WriteLine();
  35.  
  36.         foreach (var item in urls)
  37.         {
  38.             Console.WriteLine(item);
  39.         }
  40.     }
  41. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement