Dimoto

Strings, Dictionaries, Lambda and LINQ - 05. URL Parser

Jun 8th, 2016
147
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.05 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 URLParser
  9. {
  10.     static void Main()
  11.     {
  12.         string input = Console.ReadLine();
  13.         string pattern = @"(www|http|https|ftp)(\.|:\/\/)([a-z]+.[a-z]+.[a-z]+)(\/?)((.?)+)";
  14.         Regex regex = new Regex(pattern);
  15.         Match matcher = regex.Match(input);
  16.         string www = matcher.Groups[1].ToString();
  17.         //string protocol = "";
  18.         //string server = "";
  19.         //string resource = "";
  20.  
  21.         if (www.Equals("www"))
  22.         {
  23.             Console.WriteLine("[protocol] = \"\"");
  24.             Console.WriteLine("[server] = \"{0}\"", input);
  25.             Console.WriteLine("[resource] = \"\"");
  26.         }
  27.         else
  28.         {
  29.             Console.WriteLine("[protocol] = \"{0}\"", matcher.Groups[1]);
  30.             Console.WriteLine("[server] = \"{0}\"", matcher.Groups[3]);
  31.             Console.WriteLine("[resource] = \"{0}\"", matcher.Groups[5]);
  32.         }
  33.     }
  34. }
Advertisement
Add Comment
Please, Sign In to add comment