如何判断一个字符串不含了下面任何的字符?\ / " [ ] : | < > + = ; , ? * @
怎么用反义具体的写,目前不知道怎么把" [ ] 这三个字符加进来。
string pattern = @"^[^\\/@:|<>+=;,?*]+$";
if (Regex.IsMatch(username, pattern, RegexOptions.IgnoreCase) == false)
{
MessageBox.Show("NO, " + username);
}
else
{
MessageBox.Show("OK, " + username);
}
怎么用反义具体的写,目前不知道怎么把" [ ] 这三个字符加进来。
string pattern = @"^[^\\/@:|<>+=;,?*]+$";
if (Regex.IsMatch(username, pattern, RegexOptions.IgnoreCase) == false)
{
MessageBox.Show("NO, " + username);
}
else
{
MessageBox.Show("OK, " + username);
}
Regex re = new Regex(@"[\\/""\[\]\:\\|\<\>\+\=\;\,\?\*\@]", RegexOptions.None);
if(re.IsMatch("你要验证的字符串"))
{
//含用特殊字符
}
else
{
//不含用特殊字符
}
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
\W 是\w的补集
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
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
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
string username = this.textBox1.Text.Trim();
if (Regex.IsMatch(username, pattern, RegexOptions.IgnoreCase) == false)
{
MessageBox.Show("NO, " + username);
}
else
{
MessageBox.Show("OK, " + username);
}这样可以。
去掉字符串前面的@