如何判断一个字符串不含了下面任何的字符?\ / " [ ] : | < > + = ; , ? * @
怎么用反义具体的写,目前不知道怎么把" [ ] 这三个字符加进来。
string pattern = @"^[^\\/@:|<>+=;,?*]+$";
if (Regex.IsMatch(username, pattern, RegexOptions.IgnoreCase) == false)
{
       MessageBox.Show("NO, " + username);
}
 else
{
        MessageBox.Show("OK, " + username);
}

解决方案 »

  1.   


    Regex re = new Regex(@"[\\/""\[\]\:\\|\<\>\+\=\;\,\?\*\@]", RegexOptions.None);
                 
    if(re.IsMatch("你要验证的字符串"))
    {
         //含用特殊字符
    }
    else
    {
        //不含用特殊字符
    }
                     
      

  2.   

    原来是要排除,用下面这个
      static void Main(string[] args)
                {
                    string str = @"\/ "" [ ] : | cb< > + = ; , ? * @abc";                StringBuilder sb = new StringBuilder();
                    Regex re = new Regex(@"[^\\/""""\[\]\:\\|\<\>\+\=\;\,\?\*\@]", RegexOptions.IgnorePatternWhitespace);
                    MatchCollection mc = re.Matches(str);
                    foreach (Match ma in mc)
                    {
                        sb.Append(ma.Value.Trim());
                    }
                    Console.WriteLine(sb);
                    Console.ReadLine();                                
                }//输出结果:cbabc
      

  3.   

    \w   匹配任何一个数字、字母或者是下划线
    \W  是\w的补集
      

  4.   

    原来是要排除,用下面这个
      static void Main(string[] args)
                {
                    string str = @"\/ "" [ ] : | cb< > + = ; , ? * @abc";                StringBuilder sb = new StringBuilder();
                    Regex re = new Regex(@"[^\\/""""\[\]\:\\|\<\>\+\=\;\,\?\*\@]", RegexOptions.IgnorePatternWhitespace);
                    MatchCollection mc = re.Matches(str);
                    foreach (Match ma in mc)
                    {
                        sb.Append(ma.Value.Trim());
                    }
                    Console.WriteLine(sb);
                    Console.ReadLine();                                
                }//输出结果:cbabc
      

  5.   

    原来是要排除,用下面这个
      static void Main(string[] args)
                {
                    string str = @"\/ "" [ ] : | cb< > + = ; , ? * @abc";                StringBuilder sb = new StringBuilder();
                    Regex re = new Regex(@"[^\\/""""\[\]\:\\|\<\>\+\=\;\,\?\*\@]", RegexOptions.IgnorePatternWhitespace);
                    MatchCollection mc = re.Matches(str);
                    foreach (Match ma in mc)
                    {
                        sb.Append(ma.Value.Trim());
                    }
                    Console.WriteLine(sb);
                    Console.ReadLine();                                
                }//输出结果:cbabc
      

  6.   

    原来是要排除,用下面这个
      static void Main(string[] args)
                {
                    string str = @"\/ "" [ ] : | cb< > + = ; , ? * @abc";                StringBuilder sb = new StringBuilder();
                    Regex re = new Regex(@"[^\\/""""\[\]\:\\|\<\>\+\=\;\,\?\*\@]", RegexOptions.IgnorePatternWhitespace);
                    MatchCollection mc = re.Matches(str);
                    foreach (Match ma in mc)
                    {
                        sb.Append(ma.Value.Trim());
                    }
                    Console.WriteLine(sb);
                    Console.ReadLine();                                
                }//输出结果:cbabc
      

  7.   

    string pattern = "^[^\"][\\/@:|<>+=;,?*]+$";//@"^[^\\/@:|<>+=;,?*]*$";
    string username = this.textBox1.Text.Trim();
    if (Regex.IsMatch(username, pattern, RegexOptions.IgnoreCase) == false)
    {
            MessageBox.Show("NO, " + username);
    }
    else
    {
            MessageBox.Show("OK, " + username);
    }这样可以。
    去掉字符串前面的@