怎样把字符串中不是3位数字的子字符串去掉?(以空格分隔)
如str="123 46789 789"
最后得到newstr="123 789"
谢谢!

解决方案 »

  1.   

    string s = "123 46789 789";
    Regex r = new Regex(@" ?\d{4,100} ?");if (r.IsMatch(s))
    {
        s = r.Replace(s, " ");
        Console.WriteLine(s);
    }
      

  2.   

    public string AAA(string temp)
    {
    string aaa = Regex.Replace(temp,@"\d+",new MatchEvaluator(CorrectString),RegexOptions.Compiled|RegexOptions.IgnoreCase); 
    return aaa;
    }
    private static string CorrectString(Match match) 

    string matchValue = match.Value; 
    if(matchValue.Length == 3) 
    matchValue =  matchValue;
    else
    matchValue = null;
    return matchValue; 
    }
      

  3.   

    string s = "123 46789 789";
    Regex r = new Regex(@" ?\d{4,100} ?");if (r.IsMatch(s))
    {
        s = r.Replace(s, " ");
        Console.WriteLine(s);
    }
    ------------------------
    这个是错误的,只判断了四个以上的,如果有两个字符就没有替换掉,改一下参数
      

  4.   

    string  strValue = "";
    string content = @"123 46789 112 2345";
    string strRegex = "\\b\\S{4,}\\s|\\s\\S{4,}\\b";
    strValue = Regex.Replace(content, strRegex, "" , RegexOptions.IgnoreCase);
      

  5.   

    楼上 content=@"123 1 1 46789 112 2345";就错了
      

  6.   

    string  strValue = "";
    string content = @"123 1 1 46789 112 2345";
    string strRegex = "\\b(\\S{4,}|\\S{2}|\\S{1})\\s|\\s(\\S{4,}|\\S{2}|\\S{1})\\b";
    strValue = Regex.Replace(content, strRegex, "" , RegexOptions.IgnoreCase);