FindTxt="<h1>中国</h1>1月"
FindTxt = FindTxt.Replace("1", "<font color=red>" + "1" + "</font>");
我只想把1月的1变红,html标签内的不变,求指点

解决方案 »

  1.   

    您可以试试以下方式:
    string sourceString = @"<h1>中国</h1>1月";
    string pattern = @"(<h1>[^>]*>)(?<month>\d{0,2})([\u4e00-\u9fa5])";
    string result = System.Text.RegularExpressions.Regex.Replace(sourceString,pattern,@"$1<font colore=""red"">${month}</font>$2");
      
    //////////////////////////////////////////////
    MSN:[email protected]请给我一个与您交流的机会!
      

  2.   

    string ret = Regex.Replace(@"<h1>中国</h1>1月", @"(>|^)[^<]*(<|$)", new MatchEvaluator(regs1), RegexOptions.IgnoreCase);public string regs1(Match m)
    {    return m.ToString().Replace("1", "<font color=red>" + "1" + "</font>");
    }
      

  3.   

    string FindTxt=@"<h1>中国</h1>1月";
    FindTxt = Regex.Replace(FindTxt,@"(<h1>中国</h1>)(\S+?)(月)","$1<font color=red>$2</font>$3");我要分
      

  4.   

    试试:
    string sourceString = @"<h1>中国</h1>1月";
    string pattern = @"(<h1>[^>]*>)(?<month>[^<]*)";
    string result = System.Text.RegularExpressions.Regex.Replace(sourceString,pattern,@"$1<font colore=""red"">${month}</font>");
      
    //////////////////////////////////////////////
    MSN:[email protected]请给我一个与您交流的机会!
      

  5.   

    string findTxt="<h1>中国</h1>1月";
    string strRegex = @"(<h1>.*</h1>)(\d{1,2})";
    string result = Regex.Replace(FindTxt, strRegex, "$1<font color=red>$2</font>");
    Console.WriteLine("{0}", result);输出结果:<h1>中国</h1><font color=red>1</font>月
      

  6.   

    严密一点
    string findTxt="<h1>中国</h1>1月";
    string strRegex = @"(<h1>中国</h1>)(\d{1,2})(月)"; //如果"中国"不是必有的则用 .*
    string result = Regex.Replace(findTxt, strRegex, "$1<font color=red>$2</font>$3");
    Console.WriteLine("{0}", result);输出结果:<h1>中国</h1><font color=red>1</font>月