2013-04-29 50 views
2

我已经委托处理一些html代码。但我只能匹配第一个匹配。但匹配处理程序不会继续。它在第一场比赛中保持循环。谁能告诉我为什么?但是为什么我在委托之外移动匹配while循环,一切正常。奇怪的死循环在C#代表

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 
using System.Text.RegularExpressions; 
using System.Web; 
namespace migration 
{ 
    class Program 
    { 
     static void Main(string[] args) 
     { 
      string input = "<a class=\"normal\" href=\"http://msdn.microsoft.com/\" title=\"Home\">Home</a><a class=\"active\" href=\"http://msdn.microsoft.com/library/default.aspx\" title=\"Library\">Library</a><a class=\"normal\" href=\"http://msdn.microsoft.com/bb188199\" title=\"Learn\">Learn</a><a class=\"normal\" href=\"http://code.msdn.microsoft.com/\" title=\"Samples\">Samples</a><a class=\"normal\" href=\"http://msdn.microsoft.com/aa570309\" title=\"Downloads\">Downloads</a><a class=\"normal\" href=\"http://msdn.microsoft.com/hh361695\" title=\"Support\">Support</a><a class=\"normal\" href=\"http://msdn.microsoft.com/aa497440\" title=\"Community\">Community</a><a class=\"normal\" href=\"http://social.msdn.microsoft.com/forums/en-us/categories\" title=\"Forums\">Forums</a>"; 


      HTMLStringWalkThrough(input, "<a.+?</a>", "", PrintTest); 


     } 
     public static string HTMLStringWalkThrough(string HTMLString, string pattern, string replacement, HTMLStringProcessDelegate p) 
     { 
      StringBuilder sb = new StringBuilder(); 
      Match m = Regex.Match(HTMLString, pattern); 

      while (m.Success) 
      { 
       string temp = m.Value; 
       p(temp, replacement); 
       m.NextMatch(); 
      } 
      return sb.ToString(); 
     } 
     public delegate void HTMLStringProcessDelegate(string input, string replacement); 
     static void PrintTest(string tag, string replacement) 
     { 
      Console.WriteLine(tag); 
     } 
    } 
} 
//output: 
//<a class=\"normal\" href=\"http://msdn.microsoft.com/\" title=\"Home\">Home</a> 
//<a class=\"normal\" href=\"http://msdn.microsoft.com/\" title=\"Home\">Home</a> 
//<a class=\"normal\" href=\"http://msdn.microsoft.com/\" title=\"Home\">Home</a> 
//<a class=\"normal\" href=\"http://msdn.microsoft.com/\" title=\"Home\">Home</a> 
//<a class=\"normal\" href=\"http://msdn.microsoft.com/\" title=\"Home\">Home</a> 
//<a class=\"normal\" href=\"http://msdn.microsoft.com/\" title=\"Home\">Home</a> 
//<a class=\"normal\" href=\"http://msdn.microsoft.com/\" title=\"Home\">Home</a> 
//......... 
+1

[强制性警告不要使用正则表达式解析HTML内容](http://stackoverflow.com/questions/1732348/regex-match-open-tags-except -xhtml-self-contained-tags) – Servy 2013-04-29 15:35:53

回答

4

您需要将Match.NextMatch分配给您的变量。它返回的下一场比赛,并且不改变当前Match

m = m.NextMatch(); 
4

NextMatch返回下一场比赛,但你根本就不使用它的返回值。在备注部分

m = m.NextMatch(); 

请参阅the documentation,特别是:改变这一点,您的代码应工作

此方法不会修改当前实例。相反,它会返回一个新的Match对象,其中包含有关下一个匹配的信息。

+0

我很抱歉。这是我误解的事情: - \ – 2013-04-29 15:43:53

1

尝试将其更改为

 while (m.Success) 
     { 
      string temp = m.Value; 
      p(temp, replacement); 
      m = m.NextMatch(); 
     }