我已經委託處理一些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>
//.........
[強制性警告不要使用正則表達式解析HTML內容](http://stackoverflow.com/questions/1732348/regex-match-open-tags-except -xhtml-self-contained-tags) – Servy 2013-04-29 15:35:53