用于解析特定URL段的正则表达式

时间:2012-09-04 13:53:24

标签: c# regex

1 个答案:

答案 0 :(得分:1)

不太好看,但我认为你不需要正则表达式:

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类也很好,正如乔治所指出的那样。