2013-07-22 24 views
-2

我有一個url的路徑字符串,我需要操作才能找到網站內的實際頁面。找到並操作字符串

所以在網址我有這個

www.example.com/news/business/Royal寶寶 - 凱特生下男孩201306251551

我想有一些發現在網址結尾處輸入「201307231551」,然後將其放在網址中的新聞標題之前。所以,理想情況下,我會得到

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

有人可以幫助請。提前致謝。

+0

您原始的網址包含「201306251551」而非「201307231551」。我認爲這是一個錯字?另外,如果您知道URL末尾有固定長度的序列,則會有[native string methods](http://msdn.microsoft.com/zh-cn/library/system.string_methods.aspx)提取這個值很容易。 –

+3

查找[String.LastIndexOf](http://www.dotnetperls.com/lastindexof)和[String.Substring](http://www.dotnetperls.com/substring)。一旦你給了一些嘗試,如果你卡住了,就回來。 –

回答

1

爲您查找主題URL重寫ASP.NET版本。 然後,從字符串:

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

    } 
} 
+2

你向不熟悉字符串操作的人拋出了一個正則表達式?他們要比運氣好。 –

+4

是的,特別是因爲凱特已經應該在勞動。 –

+0

邁克爾是對的,我根本不熟悉正則表達式。在網上找到了關於'Regex.Replace'的一些例子,但是在正則表達式匹配後,我怎樣才能在/ 2013/07/23/15/51 /中修改'201307231551'? – Ray