【发布时间】:2009-09-14 20:04:15
【问题描述】:
在 C# 中,我尝试使用正则表达式替换查询字符串中的值。所以,如果我有:
http://www.url.com/page.aspx?id=1
我想编写一个函数,在其中传递 url、查询字符串值和要替换的值。大致如下:
string url = "http://www.url.com/page.aspx?id=1";
string newURL = ReplaceQueryStringValue(url, "id", "2");
private string ReplaceQueryStringValue(string url, string replaceWhat, string replaceWith)
{
return Regex.Replace(url, "[^?]+(?:\?"+replaceWhat+"=([^&]+).*)?",replaceWith);
}
【问题讨论】: