2

I have made a delegate to process some html code. But I can only match the first matching. But the Match handler won't proceed. It keeps looping at the first match. Can anyone tell me why? But why I move the match while loop outside the delegate, everything is OK.

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>
//.........
4

3 回答 3

4

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

m = m.NextMatch();

请参阅文档,特别是备注部分中的注释

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

于 2013-04-29T15:35:01.680 回答
4

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

 m = m.NextMatch();
于 2013-04-29T15:34:44.923 回答
1

尝试将其更改为

        while (m.Success)
        {
            string temp = m.Value;
            p(temp, replacement);
            m = m.NextMatch();
        }
于 2013-04-29T15:36:20.427 回答