如何从url字符串里面提取参数?? 求教了。。。

lijing3333 2012-03-19 01:40:45
字符串可能是:http://item.taobao.com/item.htm?id=15692320037&ali_refid=a3_420999_1007:1102857363:7:bfa050d3f458644d909956929743f117:90fa5a98c7b8bfc8a311cfe2dd088e4c&ali_trackid=1_90fa5a98c7b8bfc8a311cfe2dd088e4c&spm=874.123733.232738.14

也有可能是:http://item.taobao.com/item.htm?id=15692320037


我现在只能得到这个id的值 15692320037 是如何得到??

我看网上说用 HttpUtility.ParseQueryString 可以得到 但是我自己测试 获取不到。。。

求教了 有什么方法。。。。。
...全文
384 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
白云任去留 2012-03-19
  • 打赏
  • 举报
回复
3楼都写出来了,稍微处理下就行了!
EnForGrass 2012-03-19
  • 打赏
  • 举报
回复
string pattern = @"(?is)<h2><a href=""/ticket_19872.html"">(?<title>(.*?))</a></h2>";这句不要
EnForGrass 2012-03-19
  • 打赏
  • 举报
回复
[Quote=引用 4 楼 lijing3333 的回复:]

引用 1 楼 chinajiyong 的回复:
正则求出来你要不要?


都可以 只要能得到这个id就成。。。 我不会正则 大哥帮我写个吧!! 谢谢!!!
[/Quote]


string strfromtxt = File.ReadAllText(@"C:\1.txt", Encoding.GetEncoding("GB2312"));
string res = @"(?<=http://item.taobao.com/item.htm\?id=)(?<id>(.*?))(?=(&|$))";
string pattern = @"(?is)<h2><a href=""/ticket_19872.html"">(?<title>(.*?))</a></h2>";
MatchCollection matches = Regex.Matches(strfromtxt, res);//strfromtxt 就是你的字符串,我放到文本中的
foreach (Match match in matches)
{
string contemp = match.Groups["id"].Value.Trim();//你要的值
listpath.Add(contemp);
}
飛龍 2012-03-19
  • 打赏
  • 举报
回复
If you want use Regex
you can try this:

Regex reg=new Regex("(?<=id=)\d+");
lijing3333 2012-03-19
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 chinajiyong 的回复:]
正则求出来你要不要?
[/Quote]

都可以 只要能得到这个id就成。。。 我不会正则 大哥帮我写个吧!! 谢谢!!!
飛龍 2012-03-19
  • 打赏
  • 举报
回复

NameValueCollection qscoll = HttpUtility.ParseQueryString("http://item.taobao.com/item.htm?id=15692320037&ali_refid=a3_420999_1007:1102857363:7:bfa050d3f458644d909956929743f117:90fa5a98c7b8bfc8a311cfe2dd088e4c&ali_trackid=1_90fa5a98c7b8bfc8a311cfe2dd088e4c&spm=874.123733.232738.14");

// Iterate through the collection.
StringBuilder sb = new StringBuilder("<br />");
foreach (String s in qscoll.AllKeys)
{
sb.Append(s + " - " + qscoll[s] + "<br />");
}

白云任去留 2012-03-19
  • 打赏
  • 举报
回复
Request.QueryString["id"]啊~
EnForGrass 2012-03-19
  • 打赏
  • 举报
回复
正则求出来你要不要?

62,074

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术交流专区
javascript云原生 企业社区
社区管理员
  • ASP.NET
  • .Net开发者社区
  • R小R
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

.NET 社区是一个围绕开源 .NET 的开放、热情、创新、包容的技术社区。社区致力于为广大 .NET 爱好者提供一个良好的知识共享、协同互助的 .NET 技术交流环境。我们尊重不同意见,支持健康理性的辩论和互动,反对歧视和攻击。

希望和大家一起共同营造一个活跃、友好的社区氛围。

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