-1
它的打印雙倍,我看不到我的代碼的任何錯誤,我在正則表達式中使用匹配集合,如果找到匹配集合2(youtube鏈接)。該功能打印4張圖像。順便說一下,該函數獲得youtube thumnails。錯誤的文本替換循環輸出,意外的重複輸出
明文:
string Plainttext = "This selection is automatically generated based on videos that were popular <br/>" +
" in the past few weeks. <br/>" +
" ------------------------------------------------------------------- \n" +
" 'Anniversary Prank Backfires!! \n" +
" http://www.youtube.com/watch?v=R7AXBOT8KzU&feature=em-hot \n" +
" by RomanAtwood " +
" 23,268,129 views " +
" ------------------------------------------------------------------- \n" +
" We Are The World for Philippines [TYPHOON HAIYAN] \n" +
" http://www.youtube.com/watch?v=1bI8pGLBagY&feature=em-hot \n" +
" by Kevin Ayson " +
" 3,607,584 views <br/>" +
" ------------------------------------------------------------------- <br/>" +
" end of text ";
按鈕:
protected void Button1_Click(object sender, EventArgs e)
{
Literal1.Text = GetYoutubeThumbnails(Plainttext);
}
功能:
string geturl;// static string id ;
protected string GetYoutubeThumbnails(string plaintext)
{
var youtuberegex = new Regex("youtu(?:\\.be|be\\.com)/(?:.*v(?:/|=)|(?:.*/)?)([a-zA-Z0-9-_]+)", RegexOptions.IgnoreCase | RegexOptions.Compiled);
StringBuilder sb = new StringBuilder();
MatchCollection matches = youtuberegex.Matches(plaintext);
ArrayList arrayURL = new ArrayList(matches);
// foreach (Match match in matches)
for (int i = 0; i <= arrayURL.Count-1 ; i++)
{
geturl = arrayURL[i].ToString();
string id = string.Empty;
Match youtubeMatch = youtuberegex.Match(geturl);
id = youtubeMatch.Groups[1].Value;
// if(match.Success)
// {
sb.Append(string.Format(@"<table runat ='server'><tr> <td id = 'tduserPicture'
rowspan='1' style='text-align:left' runat='server'>
<img ID='Imagess2' runat='server' src='{0}' Height='80px' Width='100px' /alt='image'>
<br/> {1} </td> </tr></table>", "http://img.youtube.com/vi/" + id + "/0.jpg", "www." + geturl));
// }
}
plaintext = youtuberegex.Replace(plaintext, sb.ToString());
return plaintext;
}
輸出:
謝謝
的palintext是comefrom的Gmail,收件箱身,我已經redebug它,計數是正確的,但我認爲該函數獲得所有匹配並打印它,在循環條件中的每一個計數。 –
忘記了3,代碼是主要的樣例。 3只是清晰描述程序過程的示例。好的,我編輯它以避免混淆 –