URL rewriting with regular expressions
I want to extract 2 pieces of information from the URL below, "events/festivals" and "sandiego.storeboard.com".
How can I do that with a regular expression?
http://sandiego.storeboa开发者_如何学JAVArd.com/classifieds/events/festivals
I need this information for a URL rewrite in IIS 7
Try this:
^http://([^/]*)/classifieds/([^/]*/[^/]*)/
The [^/]
snippet means "everything which is not a /
"
The following C# code will retrun the two strings that you requested.
class Program
{
static void Main(string[] args)
{
GroupCollection result = GetResult("http://sandiego.storeboard.com/classifieds/events/festivals");
Console.Write(result[1] + " " + result[2]);
Console.ReadLine();
}
private static GroupCollection GetResult(string url)
{
string reg = @".*?(\w+\.\w+\.com).*?(events\/festivals)";
return Regex.Match(url, reg).Groups;
}
}
It's not the fastest solution but it works:
(.*?)/classifieds/(.*)
精彩评论