Url中的字符串string str1 = http://localhost:80/service.svc/Units
string str2 = http://localhost:80/service.svc/Units(1)
string str3 = http://localhost:80/service.svc/Units(1)/Location
string str4 = http://localhost:80/service.svc/Units(1)?$expand=Location
string str5 = http://localhost:80/service.svc/Units?$expand=Location一“/”开始到“空”或“(”或“?”结尾的字符。取出Url中的 Units 字符串。求正则写法。
string str2 = http://localhost:80/service.svc/Units(1)
string str3 = http://localhost:80/service.svc/Units(1)/Location
string str4 = http://localhost:80/service.svc/Units(1)?$expand=Location
string str5 = http://localhost:80/service.svc/Units?$expand=Location一“/”开始到“空”或“(”或“?”结尾的字符。取出Url中的 Units 字符串。求正则写法。
string reg=@"/?(\w+)[(|?]*.*";
string pattern = @"(?<=http://([^/(]*/)+)[^\s(?/]+(?=[(?\s])";
foreach (Match m in Regex.Matches(tempStr, pattern))
{
string result = m.Value;//循环输出 Units
}
string str2 = /Units(1)
string str3 = /Units(1)/Location
string str4 = /Units(1)?$expand=Location
string str5 = /Units?$expand=Location
可以说,将字符串简化一下,下面这样的字符串,应该使用什么正则获取字符串"Units"string str1 = "/Units"
string str2 = "/Units(1)"
string str3 = "/Units(1)/Location"
string str4 = "/Units(1)?$expand=Location"
string str5 = "/Units?$expand=Location"一“/”开始到“空”或“(”或“?”结尾的字符。
"/Units",
"/Units(1)",
"/Units(1)/Location",
"/Units(1)?$expand=Location",
"/Units?$expand=Location"
};
string pattern = @"(?<=/)[^\s(?/]+(?=(?>(?([(?/]).+|$)))"; string[] array = list.Select(a=>Regex.Match(a,pattern).Value).ToArray();
/*
* [0] "Units" string
[1] "Units" string
[2] "Units" string
[3] "Units" string
[4] "Units" string */
多谢,Return_false我要截取的Url地址是一个,只是有五种情况。
如果是这样的话则用