-2

我有一个来自 url 的路径字符串,我需要对其进行操作才能在网站中找到实际页面。

所以在网址中我有这个

www.example.com/news/business/Royal baby - 凯特生下男孩-201306251551

我想要在网址末尾找到“201307231551”的内容,然后将其放在网址中的新闻标题之前。所以理想情况下,我会得到

www.example.com/news/business/2013/07/23/15/51/Royal baby - 凯特生下男孩

有人可以帮忙吗。提前致谢。

4

1 回答 1

1

为您的 ASP.NET 版本查找主题 URL 重写。然后,从字符串:

www.example.com/news/business/Royal baby - 凯特生下男孩-201306251551"

您可以使用正则表达式,例如:(\d{4})(\d{2})(\d{2})(\d{2})(\d{2})。每个组代表您需要的信息部分。

祝你好运。


使用网站http://regexhero.net/tester/作为助手。

string strInputstring = @"www.example.com/news/business/Royal baby - Kate gives birth to boy-201306251551";
string strRegex = @"(\d{4})(\d{2})(\d{2})(\d{2})(\d{2})";
RegexOptions myRegexOptions = RegexOptions.None;
Regex myRegex = new Regex(strRegex, myRegexOptions);

foreach (Match myMatch in myRegex.Matches(strInputstring))
{
  if (myMatch.Success)
  {

    //myMatch.Groups[0].Value  <-  contains 2013.
    //myMatch.Groups[1].Value  <-  contains 06   
    //myMatch.Groups[2].Value  <-  contains 25   
    //myMatch.Groups[3].Value  <-  contains 15   
    //myMatch.Groups[4].Value  <-  contains 51   

  }
}
于 2013-07-22T22:49:54.257 回答