找到并操纵字符串

时间:2013-07-22 22:41:05

标签: c# asp.net

我有一段来自url的路径,我需要操作才能找到网站中的实际页面。

所以在网址中我有这个

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

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

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

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

1 个答案:

答案 0 :(得分:1)

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

  

www.example.com/news/business/Royal baby - Kate生下男孩-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   

  }
}