c# 正则表达式匹配下面的内容

epui2008 2018-01-12 05:16:59
请匹配下面内容

1 p.aspx?id=22
2 p.aspx?id=22233&title=abcd
3 p.aspx/22
4 p.aspx/22/
...全文
1828 10 打赏 收藏 转发到动态 举报
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
潜浮生 2018-02-01
  • 打赏
  • 举报
回复
引用 4 楼 epui2008 的回复:
单独匹配,不是全部匹配,不好意思,么有说清楚 我想让 http://localhost:54522/p.aspx?id=11 变成 访问 http://localhost:54522/np.aspx?id=11 global中这样写对吗? void Application_BeginRequest(object sender, EventArgs e) { Match match = Regex.Match(Context.Request.Path, @"^/p.aspx\?id=(\d+)$"); if (match.Success) { string id = match.Groups[1].Value; Context.RewritePath("/np.aspx?id=" + id); } }
引用 8 楼 epui2008 的回复:
[quote=引用 7 楼 From_TaiWan 的回复:] [quote=引用 6 楼 epui2008 的回复:] 谢谢各位大哥,我慢慢消化 现在我需要 把 /p.aspx?id=55 路由到 这里 /product.aspx?id=55 好像我这个代码写的不对 Match match2 = Regex.Match(Context.Request.Path, @"^/p.aspx?id=(\d+)$"); if (match2.Success) { string id = match2.Groups[1].Value; Context.RewritePath("/Product.aspx?id=" + id); }
改成:/p.aspx?id=(\d+) [/quote] 改了,还是不正确哦[/quote] 这个是已数字结尾的哦
秋的红果实 2018-01-29
  • 打赏
  • 举报
回复
有什么错误提示? string id = match2.Groups[1].Value; ==> 改成string id = match2.Groups[1].Value.ToString();
epui2008 2018-01-29
  • 打赏
  • 举报
回复
引用 7 楼 From_TaiWan 的回复:
[quote=引用 6 楼 epui2008 的回复:] 谢谢各位大哥,我慢慢消化 现在我需要 把 /p.aspx?id=55 路由到 这里 /product.aspx?id=55 好像我这个代码写的不对 Match match2 = Regex.Match(Context.Request.Path, @"^/p.aspx?id=(\d+)$"); if (match2.Success) { string id = match2.Groups[1].Value; Context.RewritePath("/Product.aspx?id=" + id); }
改成:/p.aspx?id=(\d+) [/quote] 改了,还是不正确哦
秋的红果实 2018-01-29
  • 打赏
  • 举报
回复
引用 6 楼 epui2008 的回复:
谢谢各位大哥,我慢慢消化 现在我需要 把 /p.aspx?id=55 路由到 这里 /product.aspx?id=55 好像我这个代码写的不对 Match match2 = Regex.Match(Context.Request.Path, @"^/p.aspx?id=(\d+)$"); if (match2.Success) { string id = match2.Groups[1].Value; Context.RewritePath("/Product.aspx?id=" + id); }
改成:/p.aspx?id=(\d+)
epui2008 2018-01-29
  • 打赏
  • 举报
回复
谢谢各位大哥,我慢慢消化 现在我需要 把 /p.aspx?id=55 路由到 这里 /product.aspx?id=55 好像我这个代码写的不对 Match match2 = Regex.Match(Context.Request.Path, @"^/p.aspx?id=(\d+)$"); if (match2.Success) { string id = match2.Groups[1].Value; Context.RewritePath("/Product.aspx?id=" + id); }
秋的红果实 2018-01-24
  • 打赏
  • 举报
回复
原来是路由配置 你的写法,只能匹配p.aspx,而且不能匹配多个get参数,例如:abcp.aspx?id=22233&title=abcd 路由要有通用性 可以这样

void Application_BeginRequest(object sender, EventArgs e)
{
    string output = Regex.Replace(Context.Request.Path, @"(\w+).aspx", "n$1");
    
    if (!string.IsNullOrEmpty(output))
    {
        Context.RewritePath(output);
    }
}

这样,abcp.aspx?id=22233&title=abcd来访,显示的是nabcp.aspx?id=22233&title=abcd
epui2008 2018-01-24
  • 打赏
  • 举报
回复
单独匹配,不是全部匹配,不好意思,么有说清楚 我想让 http://localhost:54522/p.aspx?id=11 变成 访问 http://localhost:54522/np.aspx?id=11 global中这样写对吗? void Application_BeginRequest(object sender, EventArgs e) { Match match = Regex.Match(Context.Request.Path, @"^/p.aspx\?id=(\d+)$"); if (match.Success) { string id = match.Groups[1].Value; Context.RewritePath("/np.aspx?id=" + id); } }
秋的红果实 2018-01-12
  • 打赏
  • 举报
回复
假设是固定的p.aspx文件

string input = "p.aspx?id=22AAAAAAp.aspx?id=22233&title=abcd   p.aspx/22  p.aspx/22/";
MatchCollection mCollection = Regex.Matches(input, @"(p.aspx\?id=\d+(&title=[^p]+)?)|(p.aspx/\d+/?)");

效果
  • 打赏
  • 举报
回复
\w+\.aspx(\?\w+=\w+(&\w+=\w+)*|/(\w+/?)+)$

62,041

社区成员

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

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

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

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