C#正则截取Url中的字符串

长腿爸爸 2012-07-06 09:56:48
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 字符串。

求正则写法。
...全文
398 11 打赏 收藏 转发到动态 举报
写回复
用AI写文章
11 条回复
切换为时间正序
请发表友善的回复…
发表回复
  • 打赏
  • 举报
回复
 List<string> list = new List<string>() {
"/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

*/
Kogeo_Guan 2012-07-07
  • 打赏
  • 举报
回复
"/\.svc\/(.*)/"
可以吗?
长腿爸爸 2012-07-07
  • 打赏
  • 举报
回复
[Quote=引用 7 楼 的回复:]
C# code
List<string> list = new List<string>() {
"/Units",
"/Units(1)",
"/Units(1)/Location",
"/Units(1)?$expand=Location",
……
[/Quote]

多谢,Return_false

我要截取的Url地址是一个,只是有五种情况。

三人行必有师 2012-07-07
  • 打赏
  • 举报
回复
[Quote=引用楼主 的回复:]
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 str……
[/Quote]
svc/(\w+)
如果是这样的话则用
三人行必有师 2012-07-07
  • 打赏
  • 举报
回复
[Quote=引用 8 楼 的回复:]

引用 7 楼 的回复:
C# code
List<string> list = new List<string>() {
"/Units",
"/Units(1)",
"/Units(1)/Location",
"/Units(1)?$expand=Location",
……


多谢,Return_false

我要截取的Url地址是一个,只是有五种情况。
[/Quote]

/(\w+)就可以
长腿爸爸 2012-07-06
  • 打赏
  • 举报
回复
[Quote=引用 5 楼 的回复:]
引用 4 楼 的回复:

感谢两位,我测试一下。

其实下面的字符串只剩下这样了再截取也可以,前面都是都一样的。

string str1 = /Units
string str2 = /Units(1)
string str3 = /Units(1)/Location
string str4 = /Units(1)?$expand=Location
string str5 =……
[/Quote]

可以说,将字符串简化一下,下面这样的字符串,应该使用什么正则获取字符串"Units"

string str1 = "/Units"
string str2 = "/Units(1)"
string str3 = "/Units(1)/Location"
string str4 = "/Units(1)?$expand=Location"
string str5 = "/Units?$expand=Location"

一“/”开始到“空”或“(”或“?”结尾的字符。

EnForGrass 2012-07-06
  • 打赏
  • 举报
回复
[Quote=引用 4 楼 的回复:]

感谢两位,我测试一下。

其实下面的字符串只剩下这样了再截取也可以,前面都是都一样的。

string str1 = /Units
string str2 = /Units(1)
string str3 = /Units(1)/Location
string str4 = /Units(1)?$expand=Location
string str5 = /Units?$expan……
[/Quote]
不大懂意思
长腿爸爸 2012-07-06
  • 打赏
  • 举报
回复
感谢两位,我测试一下。

其实下面的字符串只剩下这样了再截取也可以,前面都是都一样的。

string str1 = /Units
string str2 = /Units(1)
string str3 = /Units(1)/Location
string str4 = /Units(1)?$expand=Location
string str5 = /Units?$expand=Location
  • 打赏
  • 举报
回复
            string tempStr = File.ReadAllText(@"C:\Users\M\Desktop\Test.txt", Encoding.GetEncoding("GB2312"));
string pattern = @"(?<=http://([^/(]*/)+)[^\s(?/]+(?=[(?\s])";
foreach (Match m in Regex.Matches(tempStr, pattern))
{
string result = m.Value;//循环输出 Units


}
瑞卡哥哥 2012-07-06
  • 打赏
  • 举报
回复
未测试
瑞卡哥哥 2012-07-06
  • 打赏
  • 举报
回复

string reg=@"/?(\w+)[(|?]*.*";

110,549

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧