不是很漂亮,但我认为你在这里不需要正则表达式:
string Url1 = @"http://www.somesite.com/something/I-WANT-THIS-SEGMENT/cms/somethingelse";
string Url2 = @"http://www.somesite.com/something/someotherthing/I-WANT-THIS-SEGMENT/cms";
string Url3 = @"http://www.somesite.com/I-WANT-THIS-SEGMENT/cms/something";
Url1 = Url1.Substring(0, Url1.IndexOf("/cms"));
string PartOfUrl1 = Url1.Substring(Url1.LastIndexOf("/")+1);
Console.WriteLine(PartOfUrl1);
Url2 = Url2.Substring(0, Url2.IndexOf("/cms"));
string PartOfUrl2 = Url2.Substring(Url2.LastIndexOf("/")+1);
Console.WriteLine(PartOfUrl2);
Url3 = Url3.Substring(0, Url3.IndexOf("/cms"));
string PartOfUrl3 = Url3.Substring(Url3.LastIndexOf("/")+1);
Console.WriteLine(PartOfUrl3);
Uri
正如 George 指出的那样,使用这个类也很好。