C#委托中的奇怪死循环

C#委托中的奇怪死循环,c#,delegates,C#,Delegates,我委托他人处理一些html代码。但我只能匹配第一个匹配项。但匹配处理程序不会继续。它在第一场比赛中不断循环。谁能告诉我为什么?但是为什么我在循环到代理外部时移动匹配,一切都正常 using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Text.RegularExpressions; using System.Web; namespace migration

我委托他人处理一些html代码。但我只能匹配第一个匹配项。但匹配处理程序不会继续。它在第一场比赛中不断循环。谁能告诉我为什么?但是为什么我在循环到代理外部时移动匹配,一切都正常

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>
//.........
使用系统;
使用System.Collections.Generic;
使用System.Linq;
使用系统文本;
使用System.Text.RegularExpressions;
使用System.Web;
命名空间迁移
{
班级计划
{
静态void Main(字符串[]参数)
{
字符串输入=”;
HTMLStringWalkThrough(输入,“
//
//
//
//
//
//
//.........
您需要为变量赋值。它返回下一个匹配项,并且不会更改当前的
匹配项

 m = m.NextMatch();

NextMatch
返回下一个匹配项,但您根本不使用它的返回值。更改此项,您的代码应该可以工作:

m = m.NextMatch();
请参见备注部分的注释:

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

试着把它改成

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

我很抱歉,这就是我看错的地方:-\