谢谢拉
如替换 "four four{ ccfour ddffdfourdf score and seven fourr four four() fourU years Four ago";中的four,而ccfour ,ddffdfourdf, fourr 
,fourU ,Four 不做替换,而four 和four() 都替换的

解决方案 »

  1.   

    如上面的把four 替换为Six应该得到以下结果“six six{ ccfour ddffdfourdf score and seven fourr six six() fourU years Four ago”
      

  2.   

    string s = "four four{ ccfour ddffdfourdf score and seven fourr four four() fourU years Four ago";

    s = Regex.Replace(s, @"\bfour\b", "six");
    Console.WriteLine(s);
      

  3.   

    用正则表达式:
    using System.Text.RegularExpressions;private void button1_Click(object sender, System.EventArgs e)
    {
    string s=@"four four{ ccfour ddffdfourdf score and seven fourr four four() fourU years Four ago";
    string myreg=@"\bfour(\(\)){0,1}\b";  
    string a=Regex.Replace(s,myreg,new MatchEvaluator(this.mydo));
               this.textBox1.Text=a;
    } private string mydo(Match m)
    {
                string a=m.Value;
    if(a.IndexOf("()")>0)

    return "six()";
    else
    return "six";
    }
      

  4.   

    string Text = "four four{ ccfour ddffdfourdf score and seven fourr four four() fourU years Four ago";string  Pattern=@"\bfour\b";
    Text= Regex.Replace(s, Pattern,"six"); 同意楼上的做法,\b是字符边界,而在这里只要控制其边界就对了!
      

  5.   

    private void button1_Click(object sender, System.EventArgs e)
    {
    string s=@"four four{ ccfour ddffdfourdf score and seven fourr four four() fourU years Four ago";
    string myreg=@"\bfour\b";  
    string a=Regex.Replace(s,myreg,new MatchEvaluator(this.mydo));
               this.textBox1.Text=a;
    } private string mydo(Match m)
    {
                string a=m.Value;
    return "six";
    }
      

  6.   

    思归的能替换four()吗?试了试,可以。看来()不算字。
      

  7.   

    \bfour\b//思归大哥,是不是没有处理 "_" 字符;
      

  8.   

    public string ReplaceString(string MainString,string OldString, string NewString)
    {

    if(MainString.Length > 0)
    {             
    MainString = MainString.Replace(OldString,NewString);
    }
    return MainString;
    }
    //MainString 为主字符串
    //OldString 为要替换的字符串
    //NewString 为要替换成的新串
    测试:
    string RepString="four four{ ccfour ddffdfourdf score and seven fourr four four() fourU years Four ago";
          RepString =  ReplaceString(RepString,"four ", "six ");
    //RepString = six four{ ccsix ddffdfourdf score and seven fourr six four() fourU years Four ago
           RepString = ReplaceString(RepString,"four()", "six()");
    //“six six{ ccfour ddffdfourdf score and seven fourr six six() fourU years Four ago”
    //注意输入的字符串的写法
      

  9.   

    按楼主的意思,像如 _*four_* 这种情况,应该是替换掉的.
    但\bfour\b 
    是不会匹配_*four_* 中的"four"的.此贴最简捷的正则表达式写法怎么写?
      

  10.   

    >>>但\bfour\b 是不会匹配_*four_* 中的"four"的the above suggestion is based on the literal string, if this is the new requirement, then you have to try something likestring s = "four four{ ccfour ddffdfourdf score and seven fourr four four() fourU years Four ago _*four_* ";

    s = Regex.Replace(s, @"(?<![a-zA-Z0-9])four(?![a-zA-Z0-9])", "six");

    Console.WriteLine(s);
      

  11.   

    哈哈,saucer(思归)是请过来的,谢谢大家,先结帖再测试。