Advertisement
stanevplamen

02.8a.07.2.WebExtraction

Jun 14th, 2013
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.77 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6.  
  7. class DomainNameExtract
  8. {
  9.     /*
  10.     ** Method 1 (using the build-in Uri-object)
  11.     */
  12.     public static string ExtractDomainNameFromURL_Method1(string Url)
  13.     {
  14.         if (!Url.Contains("://"))
  15.             Url = "http://" + Url;
  16.  
  17.         return new Uri(Url).Host;
  18.     }
  19.  
  20.     /*
  21.     ** Method 2 (using string modifiers)
  22.     */
  23.     public static string ExtractDomainNameFromURL_Method2(string Url)
  24.     {
  25.         if (Url.Contains(@"://"))
  26.             Url = Url.Split(new string[] { "://" }, 2, StringSplitOptions.None)[1];
  27.  
  28.         return Url.Split('/')[0];
  29.     }
  30.  
  31.     /*
  32.     ** Method 3 (using regular expressions -> slowest)
  33.     */
  34.     public static string ExtractDomainNameFromURL_Method3(string Url)
  35.     {
  36.         return System.Text.RegularExpressions.Regex.Replace(
  37.             Url,
  38.             @"^([a-zA-Z]+:\/\/)?([^\/]+)\/.*?$",
  39.             "$2"
  40.         );
  41.     }
  42.  
  43.     static void Main()
  44.     {
  45.         // Some example urls:
  46.         string[] Urls = new string[]
  47.         {
  48.         "http://www.somedomain.bg/snippets/csharp/",
  49.         "www.somedomain.bg/snippets/csharp/",
  50.         "http://www.somedomain.bg/",
  51.         "ftp://www.somedomain.bg/",
  52.         "www.somedomain.bg/",
  53.         "https://subdomain.abc.def.somedomain.bg/test.htm"
  54.         };
  55.  
  56.         // Test all urls with all different methods:
  57.         foreach (string Url in Urls)
  58.         {
  59.             Console.WriteLine("Method 1: {0}", ExtractDomainNameFromURL_Method1(Url));
  60.             Console.WriteLine("Method 2: {0}", ExtractDomainNameFromURL_Method2(Url));
  61.             Console.WriteLine("Method 3: {0}", ExtractDomainNameFromURL_Method3(Url));
  62.         }
  63.     }
  64. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement