正则表达式替换URL中的页码

时间:2021-11-05 08:29:12

I'm not good with regex and I'm not able to figure out an applicable solution, so after a good amount of searching I'm still unable to nail this.

我对正则表达式并不擅长,而且我无法找到适用的解决方案,所以在经过大量搜索之后,我仍然无法确定这一点。

I have an URL with an optional page=123 parameter. There can be other optional get parameters in the url too which can occur before or after the page parameter.

我有一个带有可选page = 123参数的URL。 url中也可以有其他可选的get参数,这些参数可以在page参数之前或之后发生。

I need to replace that parameter to page=--PLACEHOLDER-- to be able to use it with my paging function.

我需要将该参数替换为page = - PLACEHOLDER--以便能够将其与我的分页功能一起使用。

If the page parameter does not occur in the url, I would like to add it the way described before.

如果页面参数没有出现在url中,我想按照之前描述的方式添加它。

I'm trying to write an extension method for on string for this, but a static function would be just as good.

我正在尝试为字符串编写一个扩展方法,但静态函数也同样好。

A bit of explanation would be appreciated too, as it would give me a good lesson in regex and hopefully next time I wouldn't have to ask.

一点点解释也会受到赞赏,因为它会给我一个很好的正则表达式的教训,并希望下次我不必问。

Also I'm using asp.net mvc-3 but for compatibility reasons a complex rewrite occurs before the mvc-s routing, and I'm not able to access that. So please don't advise me to use mvc-s routing for this because I cant.

我也使用asp.net mvc-3但是出于兼容性原因,在mvc-s路由之前发生了复杂的重写,我无法访问它。所以请不要建议我使用mvc-s路由,因为我不能。

2 个解决方案

#1


5  

I suggest skipping the regex and using another approach:

我建议跳过正则表达式并使用另一种方法:

  1. Extract the querystring from the url.
  2. 从url中提取查询字符串。

  3. Build a HttpValueCollection from the querystring using HttpUtility.ParseQueryString
  4. 使用HttpUtility.ParseQueryString从查询字符串构建HttpValueCollection

  5. Replace the page parameter in the collection.
  6. 替换集合中的page参数。

  7. Call .ToString() on the collection and you get a new querystring back.
  8. 在集合上调用.ToString(),然后返回一个新的查询字符串。

  9. Construct the altered url using the original minus the old querystring plus the new one.
  10. 使用原始减去旧查询字符串加上新查询字符串构造更改后的URL。

Something like:

public static string SetPageParameter(this string url, int pageNumber)
{
    var queryStartIndex = url.IndexOf("?") + 1;
    if (queryStartIndex == 0)
    {
        return string.Format("{0}?page={1}", url, pageNumber);
    }
    var oldQueryString = url.Substring(queryStartIndex);
    var queryParameters = HttpUtility.ParseQueryString(oldQueryString);
    queryParameters["page"] = pageNumber;
    return url.Substring(0, queryStartIndex) + queryParameters.ToString();
}

I haven't verified that this compiles, but it should give you an idea.

我没有证实这个编译,但它应该给你一个想法。

#2


0  

You want it as a static method with regular expression, here is a first state :

你想要它作为一个带有正则表达式的静态方法,这里是第一个状态:

public static string ChangePage(string sUrl)
{
  string sRc = string.Empty;
  const string sToReplace = "&page=--PLACEHOLDER--";

  Regex regURL = new Regex(@"^http://.*(&?page=(\d+)).*$");

  Match mPage =  regURL.Match(sUrl);
  if (mPage.Success) {
    GroupCollection gc = mPage.Groups;
    string sCapture = gc[1].Captures[0].Value;
    // gc[2].Captures[0].Value) is the page number
    sRc = sUrl.Replace(sCapture, sToReplace);
  }
  else {
    sRc = sUrl+sToReplace;
  }

  return sRc;
}

With a small test :

通过一个小测试:

static void Main(string[] args)
{
  string sUrl1 = "http://localhost:22666/HtmlEdit.aspx?mid=0&page=123&test=12";
  string sUrl2 = "http://localhost:22666/HtmlEdit.aspx?mid=0&page=125612";
  string sUrl3 = "http://localhost:22666/HtmlEdit.aspx?mid=0&pager=12";
  string sUrl4 = "http://localhost:22666/HtmlEdit.aspx?page=12&mid=0";

  string sRc = string.Empty;
  sRc = ChangePage(sUrl1);
  Console.WriteLine(sRc);
  sRc = ChangePage(sUrl2);
  Console.WriteLine(sRc);
  sRc = ChangePage(sUrl3);
  Console.WriteLine(sRc);
  sRc = ChangePage(sUrl4);
  Console.WriteLine(sRc);
}

which give the result :

给出了结果:

http://localhost:22666/HtmlEdit.aspx?mid=0&page=--PLACEHOLDER--&test=12
http://localhost:22666/HtmlEdit.aspx?mid=0&page=--PLACEHOLDER--
http://localhost:22666/HtmlEdit.aspx?mid=0&pager=12&page=--PLACEHOLDER--
http://localhost:22666/HtmlEdit.aspx?&page=--PLACEHOLDER--&mid=0

#1


5  

I suggest skipping the regex and using another approach:

我建议跳过正则表达式并使用另一种方法:

  1. Extract the querystring from the url.
  2. 从url中提取查询字符串。

  3. Build a HttpValueCollection from the querystring using HttpUtility.ParseQueryString
  4. 使用HttpUtility.ParseQueryString从查询字符串构建HttpValueCollection

  5. Replace the page parameter in the collection.
  6. 替换集合中的page参数。

  7. Call .ToString() on the collection and you get a new querystring back.
  8. 在集合上调用.ToString(),然后返回一个新的查询字符串。

  9. Construct the altered url using the original minus the old querystring plus the new one.
  10. 使用原始减去旧查询字符串加上新查询字符串构造更改后的URL。

Something like:

public static string SetPageParameter(this string url, int pageNumber)
{
    var queryStartIndex = url.IndexOf("?") + 1;
    if (queryStartIndex == 0)
    {
        return string.Format("{0}?page={1}", url, pageNumber);
    }
    var oldQueryString = url.Substring(queryStartIndex);
    var queryParameters = HttpUtility.ParseQueryString(oldQueryString);
    queryParameters["page"] = pageNumber;
    return url.Substring(0, queryStartIndex) + queryParameters.ToString();
}

I haven't verified that this compiles, but it should give you an idea.

我没有证实这个编译,但它应该给你一个想法。

#2


0  

You want it as a static method with regular expression, here is a first state :

你想要它作为一个带有正则表达式的静态方法,这里是第一个状态:

public static string ChangePage(string sUrl)
{
  string sRc = string.Empty;
  const string sToReplace = "&page=--PLACEHOLDER--";

  Regex regURL = new Regex(@"^http://.*(&?page=(\d+)).*$");

  Match mPage =  regURL.Match(sUrl);
  if (mPage.Success) {
    GroupCollection gc = mPage.Groups;
    string sCapture = gc[1].Captures[0].Value;
    // gc[2].Captures[0].Value) is the page number
    sRc = sUrl.Replace(sCapture, sToReplace);
  }
  else {
    sRc = sUrl+sToReplace;
  }

  return sRc;
}

With a small test :

通过一个小测试:

static void Main(string[] args)
{
  string sUrl1 = "http://localhost:22666/HtmlEdit.aspx?mid=0&page=123&test=12";
  string sUrl2 = "http://localhost:22666/HtmlEdit.aspx?mid=0&page=125612";
  string sUrl3 = "http://localhost:22666/HtmlEdit.aspx?mid=0&pager=12";
  string sUrl4 = "http://localhost:22666/HtmlEdit.aspx?page=12&mid=0";

  string sRc = string.Empty;
  sRc = ChangePage(sUrl1);
  Console.WriteLine(sRc);
  sRc = ChangePage(sUrl2);
  Console.WriteLine(sRc);
  sRc = ChangePage(sUrl3);
  Console.WriteLine(sRc);
  sRc = ChangePage(sUrl4);
  Console.WriteLine(sRc);
}

which give the result :

给出了结果:

http://localhost:22666/HtmlEdit.aspx?mid=0&page=--PLACEHOLDER--&test=12
http://localhost:22666/HtmlEdit.aspx?mid=0&page=--PLACEHOLDER--
http://localhost:22666/HtmlEdit.aspx?mid=0&pager=12&page=--PLACEHOLDER--
http://localhost:22666/HtmlEdit.aspx?&page=--PLACEHOLDER--&mid=0