是这样 我想判断一个字符在一个字符串中只出现过一次 如何写呢
例如:a...b...c...b...c..d
如果b出现两次就为否谢谢

解决方案 »

  1.   

    何必用正则呢
    string str = "dfdf324rdfsf";
    char givenChar = 'd';
    if(str.IndexOf(givenChar) == str.LastIndexOf(givenChar))
    {
        //只出现一次
    }
    else
    {
        //出现多于一次
    }
      

  2.   

    谢谢各位大虾的回答
    我原来用的是
    ^[\s\S]*(a){1}[\s\S]*(b){1}[\s\S]*(c){1}[\S\s]*$
    因为至少要保证,a,b,c出现一次 但不能是两次,加了[^a]*在中间后感觉还是不行
    这样。。^[\s\S]*[^a]*(a){1}[\s\S]*[^b]*(b){1}[\s\S]*[^c]*(c){1}[\S\s]*$
      

  3.   


    //可以用linQ
                string str = @"a...b...c...b...c..d";            Char testchar='a';            var _re = from s in str.ToCharArray()
                          group s by s into gp
                          where gp.Count() == 1 && gp.Key == testchar
                          select gp.Key;            string _result = "";            _result = _re.Count() > 0 ? "Yes" : "No";