比如我在一个正则表达式中匹配出一项,我想将这一项匹配修改为其他内容
比如有如下数据
a=2;b=3;c=4;d=3;我现在想修改b的值,按的我正则表达式可以成功得到b的值的匹配项为3,然后我想修改该匹配项的值为6,请问如何修改我只能Matche得到值为3,但是我不知道怎么将原字符串其改成6请不要简单用replace函数将3这个字符替换,因为还有可能其他地方出现3这个字符,谢谢各位了

解决方案 »

  1.   

    string s="a=2;b=3;c=4;d=3;";
    Regex rg=new Regex("(b=)(3)");
    s=rg.Replace(s, delegate(Match m){return m.Groups[1]+"6";});
    Console.WriteLine("After replace: {0}", s);
      

  2.   

    static void Main(string[] args)
            {
                string s = "a=2;b=3;c=4;d=3;";
                Regex rx = new Regex(@"b=(?<value>\d+)");
                //得到值 
                string str = rx.Match(s).Result("${value}");
                Console.WriteLine(str);
                //替换为6
                str=rx.Replace(s, new MatchEvaluator(CapText));
                Console.WriteLine(str );        }
            static string CapText(Match m)
            {
                return "6";
            }
      

  3.   

    using System.Text.RegularExpressions;
      

  4.   

    string s = "a=2;b=3;c=4;d=3;";
    Regex rx = new Regex(@"b=(\d+)");
    Match m=rx.Match(s);
    string a=m.Groups[0].Value;
    string b=m.Groups[1].Value;
    s=s.Replace(a,a.Replace(b,"6"));