如何不允许特殊字符,但在正则表达式中允许空格? [英] How do I not allow special characters, but allow space in regex?

查看:148
本文介绍了如何不允许特殊字符,但在正则表达式中允许空格?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当用户在datagridview中输入特殊字符时,我需要填充一条错误消息.截至目前,我有以下内容:

I need to populate an error message when the user is entering special characters within a datagridview. as of now I have the following:

if (Regex.IsMatch(columnValue.ToString(),"^[A-Za-z0-9]$"))
                {
                    MessageBox.Show("You may not use special characters.", "Saving not allowed at this time", MessageBoxButtons.OK, MessageBoxIcon.Error);  

此正则表达式有效,并且不允许特殊字符,但是如果字符之间存在空格,则应放在if语句中.

this regex works, and does not allow special character, however it is falls in the if statement if there is a space between characters, which should be allowed.

如何使其允许有空格,但不能包含特殊字符?

How do I make it to allow a space, but not special characters?

谢谢

推荐答案

我认为您只想在正则表达式中添加一个空格:

I think you just want to add a space to your regular expression:

if (Regex.IsMatch(columnValue.ToString(),"^[ A-Za-z0-9]$"))

在编写正则表达式时,通常习惯使用逐字字符串运算符 @ ,因为许多特殊表达式都要求您使用反斜杠字符.

When writing regular expressions it is generally a good habit to use the verbatim string operator @ as many of the special expressions require you to use a backslash character.

if (Regex.IsMatch(columnValue.ToString(), @"^[ A-Za-z0-9]$"))

如果您要明确告诉正则表达式忽略模式中的空格,则需要转义空格字符.

If you were explicitly telling the regular expression to ignore whitespace in the pattern you would then need to escape the space character.

if (Regex.IsMatch(columnValue.ToString(), @"^[\ A-Za-z0-9]$", RegexOption.IgnorePatternWhitespace))

如果希望他们能够使用任何空格字符(空格,制表符,换行符等),则可以使用空格字符类 \ s

If you wanted them to be able to use any whitespace characters (spaces, tabs, newlines, etc.), you could use the whitespace character class \s

if (Regex.IsMatch(columnValue.ToString(), @"^[\sA-Za-z0-9]$"))


更新

您似乎想要一个正则表达式,该正则表达式排除了组中的字符.

It appears that you may have wanted to a regular expression that excluded characters in a group.

您当前的正则表达式从字符串 ^ 的开始到字符串 $ 的末尾查找组 A-Za中的一个字符-z0-9 .如果要匹配完全相反的内容,可以在 Regex.IsMatch 之前使用not运算符,例如:

Your current regular expression is looking from the beginning of the string ^ to the end of the string $ for exactly one character in the group A-Za-z0-9. If you want to match the exact opposite you could use the not operator ! before your Regex.IsMatch, like:

if (!Regex.IsMatch(columnValue.ToString(), @"^[ A-Za-z0-9]$"))

但是,如果您想在正则表达式中写入排除的字符组,则可以在组中放入 ^ .当 ^ 在组 [^] 中时,其后的所有字符都将从匹配中排除.

But if you wanted to write an excluded group of characters in your regular expression you can put a ^ in your group. When the ^ is inside of a group [^] any characters that come after it are excluded from the match.

正则表达式 [A-Z] 将与任何字符A-Z匹配.正则表达式 [^ A-Z] 可以匹配任何字符 NOT A-Z.

The regular expression [A-Z] would match any character A-Z. The regular expression [^A-Z] would match any character NOT A-Z.

如果这就是您要查找的内容,则可以将您的声明更改为:

If that is what you were looking for you could change your statement to:

if (Regex.IsMatch(columnValue.ToString(), @"[^A-Za-z0-9\ ]"))

此语句将匹配包含任何不在组中的字符的任何字符串.因此,如果他们键入"Hello World",则将不匹配.如果他们键入"Hello!World",它将匹配,因为!"不在群组中.

This statement will match any string that contains any character not in the group. So if they type "Hello World" it would not match. If they typed "Hello! World" it would match because "!" is not in the group.

注意:开头的 ^ 和结尾的 $ 已删除.在此正则表达式中,我们不在乎字符串的长度,因为我们只匹配不希望在字符串中任何位置出现的字符.

NOTE: The leading ^ and trailing $ were removed. In this regular expression we don't care about the length of the string as we are only matching on a character we were not expecting anywhere in the string.

这篇关于如何不允许特殊字符,但在正则表达式中允许空格?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆