【发布时间】:2011-09-03 11:04:40
【问题描述】:
我想从下面的 URL 中提取 2 条信息,“events/festivals”和“sandiego.storeboard.com”。
如何用正则表达式做到这一点?
http://sandiego.storeboard.com/classifieds/events/festivals
我需要这些信息来在 IIS 7 中重写 URL
【问题讨论】:
标签: regex iis-7 url-rewriting
我想从下面的 URL 中提取 2 条信息,“events/festivals”和“sandiego.storeboard.com”。
如何用正则表达式做到这一点?
http://sandiego.storeboard.com/classifieds/events/festivals
我需要这些信息来在 IIS 7 中重写 URL
【问题讨论】:
标签: regex iis-7 url-rewriting
试试这个:
^http://([^/]*)/classifieds/([^/]*/[^/]*)/
[^/] sn-p 的意思是“所有不是/”
【讨论】:
以下 C# 代码将重新运行您请求的两个字符串。
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;
}
}
【讨论】:
这不是最快的解决方案,但它有效:
(.*?)/classifieds/(.*)
【讨论】: