查找和操作字符串

本文关键字:字符串 操作 查找 | 更新日期: 2023-09-27 18:24:24

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

所以在url中我有这个

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

我想在url的末尾找到"201307231551",然后把它放在url的新闻标题之前。所以理想情况下,我会得到

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

有人能帮忙吗。提前谢谢。

查找和操作字符串

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

www.example.com/news/business/皇家宝宝-凯特生下男孩-20130621551"

您可以使用类似于:('d{4})('d{2})('d{2})('d{2})('d{2})的Regex。每组代表您需要的信息部分。

祝你好运。


已使用网站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   
  }
}