2013-12-12 68 views
-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; 
     } 

輸出: enter image description here

謝謝

+0

的palintext是comefrom的Gmail,收件箱身,我已經redebug它,計數是正確的,但我認爲該函數獲得所有匹配並打印它,在循環條件中的每一個計數。 –

+0

忘記了3,代碼是主要的樣例。 3只是清晰描述程序過程的示例。好的,我編輯它以避免混淆 –

回答

1

問題是你修改你的循環內的文本,應該創建一個單獨的輸出,而不是一個就地替換。

甚至不用這樣做,使用Regex.Replace callback function

撞成這樣一起作爲一個超快速的樣品

using System.IO; 
using System; 
using System.Collections; 
using System.Text.RegularExpressions; 


class Program 
{ 
    static void Main() 
    { 

     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 "; 

     MatchEvaluator evaluator = new MatchEvaluator(MatchFilter); 

     Console.WriteLine(Regex.Replace(Plainttext, 
     "youtu(?:\\.be|be\\.com)/(?:.*v(?:/|=)|(?:.*/)?)([a-zA-Z0-9-_]+)", evaluator, 
             RegexOptions.IgnorePatternWhitespace)); 
    } 

    static string MatchFilter(Match match) { 
     var id = match.Groups[1].Value; 
     var geturl = match.ToString(); 
     return 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); 
    } 
} 

它輸出以下內容:在我原來的計劃

This selection is automatically generated based on videos that were popular <br/> in the past few weeks. <br/> ------------------------------------------------------------------- 
'Anniversary Prank Backfires!! 
http://www.<table runat ='server'><tr> <td id = 'tduserPicture' 
        rowspan='1' style='text-align:left' runat='server'> 
        <img ID='Imagess2' runat='server' src='http://img.youtube.com/vi/R7AXBOT8KzU/0.jpg' Height='80px' Width='100px' /alt='image'> 
       <br/> www.youtube.com/watch?v=R7AXBOT8KzU </td> </tr></table>&feature=em-hot 
by RomanAtwood 23,268,129 views ------------------------------------------------------------------- 
We Are The World for Philippines [TYPHOON HAIYAN] 
http://www.<table runat ='server'><tr> <td id = 'tduserPicture' 
        rowspan='1' style='text-align:left' runat='server'> 
        <img ID='Imagess2' runat='server' src='http://img.youtube.com/vi/1bI8pGLBagY/0.jpg' Height='80px' Width='100px' /alt='image'> 
       <br/> www.youtube.com/watch?v=1bI8pGLBagY </td> </tr></table>&feature=em-hot 
by Kevin Ayson 3,607,584 views <br/> ------------------------------------------------------------------- <br/> end of text 
+0

感謝您完美的工作...... GodBless –