her er en regex variant:
using System;
using System.Text.RegularExpressions;
namespace E
{
public class MainClass
{
private static Regex re = new Regex(@"([a-z]+)(?:://)((([\w]+):([\w]+)@)|)([\w-\.]+)((/[\w\-/\.]*)|)");
public static void Test(string url)
{
Match m = re.Match(url);
string protocol = m.Groups[1].Value;
string user = m.Groups[4].Value;
string pass = m.Groups[5].Value;
string host = m.Groups[6].Value;
string path = m.Groups[8].Value;
Console.WriteLine("URL = " + url);
Console.WriteLine("protocol = " + protocol);
Console.WriteLine("user = " + user);
Console.WriteLine("pass = " + pass);
Console.WriteLine("host = " + host);
Console.WriteLine("path = " + path);
}
public static void Main(string[] args)
{
Test("
http://www.eksperten.dk");
Test("
www.myplace.dk"" target="_blank">http://adminuser:adminpass@
www.myplace.dk");
Test("
https://www.eksperten.dk");
Test("
http://www.eksperten.dk/spm/770525");
Console.ReadLine();
}
}
}
den kan sikert forbedres yderligere, men det var hvad mine regex evener rækker til