abcd123 <-这里的123要删除
ab&#123 <-这里的123不删除

解决方案 »

  1.   


                string str = "abcd123";
                string str1 = "ab&#123";
                string result = Regex.Replace(str, @"(&#)?\d+", delegate(Match m) { if (string.IsNullOrEmpty(m.Groups[1].Value))return ""; else return m.Value; });
                string result1 = Regex.Replace(str1, @"(&#)?\d+", delegate(Match m) { if (string.IsNullOrEmpty(m.Groups[1].Value))return ""; else return m.Value; });
                Response.Write(result + "<br/>");
                Response.Write(result1 + "<br/>");
    /*
    abcd
    ab&#123
    */
      

  2.   


                string s = "abcd123";
                string result = "";
                Match ma = Regex.Match(s, @"(&#\d+)");
                if (ma.Groups.Count == 1)
                {
                    result = Regex.Replace(s, @"\d+", "");
                }
                MessageBox.Show(result);
      

  3.   

    谢谢两位,但是我觉得你们的方法是不是有些复杂呢?
    我用正则表达式“(?<!&#)(\d+)\b” 
    问题如下:
    "abcd123" 没有问题,结果是abc
    "ab&#123" 出现问题,结果是ab&#1  想要的结果是ab&#123
      

  4.   

    不用正则
    if("ab&#123".StartWith("&#"))
    {
     ....
    }
      

  5.   

    你的正则修改成(?<!&#\d*)(\d+)\b也可以