Get Base / Root URL

  • Thread starter Thread starter A_StClaire_
  • Start date Start date
hi,

dumb question. is there a .NET function that gets me the string
"http://msn.com" if I feed it stuff like "http://msn.com/help" or
"http://msn.com/msnmessenger"?

thx

You can do that with a regular expression

e.g.

string[] test = { @"http://www.msn.com/help/",
@"www.msn.com/help/",
@"https://www.example.com/somepage.php",
@"hTtP://foo.bar.com/test.html",
@"htTpS://examplepage.com/foo"
};

string regex =
@"([hH][tT]{2}[pP][sS]?://|[fF][tT][pP]://)?[a-z0-9-]*\.?[a-zA-Z0-9-]{0,}\.?[a-zA-Z0-9-]{0,}";

foreach (string s in test)
Console.WriteLine(Regex.Match(s, regex));


Output:

http://www.msn.com
www.msn.com
https://www.example.com
hTtP://foo.bar.com
htTpS://examplepage.com

Hope that helps,
Bry
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top