日期:2014-05-19  浏览次数:20753 次

正则表达的替换如何跟参数
using   System.Text.RegularExpressions;

class   RegExSample  
{
      static   string   CapText(Match   m)  
      {
            //   Get   the   matched   string.
            string   x   =   m.ToString();
            //   If   the   first   char   is   lower   case...
            if   (char.IsLower(x[0]))  
            {
                  //   Capitalize   it.
                  return   char.ToUpper(x[0])   +   x.Substring(1,   x.Length-1);
            }
            return   x;
      }
       
      static   void   Main()  
      {
            string   text   =   "four   score   and   seven   years   ago ";
            System.Console.WriteLine( "text=[ "   +   text   +   "] ");
            string   result   =   Regex.Replace(text,   @ "\w+ ",
                  new   MatchEvaluator(RegExSample.CapText));
            System.Console.WriteLine( "result=[ "   +   result   +   "] ");
      }
}

上面一段代码是在msdn中找到的,现在要做的是如何在   CapText方法中传进去一个参数。

------解决方案--------------------
only mark

等过客..
------解决方案--------------------
看你要实现什么样的功能,达到什么样的效果吧

正则中使用委托,如果需要传参数的话,我一般是用全局变量来做的

下面这个帖子和后面帖子最后的回复都用到的全局变量,可以参考一下
http://community.csdn.net/Expert/topic/5602/5602978.xml?temp=.7702295
http://community.csdn.net/Expert/topic/5600/5600177.xml?temp=.0136835

或者楼主可以说下自己的目的,我给看下方法吧