C# 我如何不允许特殊字符,但在正则表达式中允许空格?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14106109/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How do I not allow special characters, but allow space in regex?
提问by user1683987
I need to populate an error message when the user is entering special characters within a datagridview. as of now I have the following:
当用户在 datagridview 中输入特殊字符时,我需要填充错误消息。截至目前,我有以下几点:
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);
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.
这个正则表达式有效,并且不允许特殊字符,但是如果字符之间有空格,则它属于 if 语句,应该允许。
How do I make it to allow a space, but not special characters?
我如何让它允许一个空格,但不允许特殊字符?
Thanks,
谢谢,
采纳答案by Jason Whitted
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))
If you wanted them to be able to use any whitespace characters (spaces, tabs, newlines, etc.), you could use the whitespace character class \s
如果您希望它们能够使用任何空白字符(空格、制表符、换行符等),您可以使用空白字符类 \s
if (Regex.IsMatch(columnValue.ToString(), @"^[\sA-Za-z0-9]$"))
Update
更新
It appears that you may have wanted to a regular expression that excluded characters in a group.
看来您可能想要一个排除组中字符的正则表达式。
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:
您当前的正则表达式正在从字符串的开头到字符串^
的末尾$
查找组中正好有一个字符A-Za-z0-9
。如果你想匹配完全相反的你可以!
在你的 之前使用 not 运算符Regex.IsMatch
,例如:
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.
但是如果你想在你的正则表达式中写入一个排除的字符组,你可以^
在你的组中放置一个。当^
位于组内时,[^]
它之后的任何字符都将从匹配中排除。
The regular expression [A-Z]
would match any character A-Z. The regular expression [^A-Z]
would match any character NOTA-Z.
正则表达式[A-Z]
将匹配任何字符 AZ。正则表达式[^A-Z]
将匹配任何字符NOTAZ。
If that is what you were looking for you could change your statement to:
如果这就是您要查找的内容,您可以将您的声明更改为:
if (Regex.IsMatch(columnValue.ToString(), @"[^A-Za-z0-9\ ]"))
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.
此语句将匹配包含任何不在组中的任何字符的任何字符串。因此,如果他们输入“Hello World”,它将不匹配。如果他们输入“Hello!World”,它会匹配,因为“!” 不在组中。
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.
注意:前导^
和尾随$
已删除。在这个正则表达式中,我们不关心字符串的长度,因为我们只匹配字符串中我们不希望出现的任何字符的字符。
回答by SWeko
Add a space in the character class, i.e. use ^[A-Za-z0-9 ]$
instead of ^[A-Za-z0-9]$
. This will still disallow other whitespace characters, like tab
.
在字符类中添加一个空格,即使用^[A-Za-z0-9 ]$
而不是^[A-Za-z0-9]$
. 这仍将禁止其他空白字符,例如tab
.
回答by bonCodigo
You could include all the digits, strings and space :)
您可以包含所有数字、字符串和空格:)
"^[a-z]|[A-Z]|[0-9]|[ ]$"
or
或者
"^[\d+]|[\w+]|[ ]$"
回答by Aniket Inge
Why not try this regex: [a-zA-Z0-9\s]+
为什么不试试这个正则表达式: [a-zA-Z0-9\s]+
回答by MethodMan
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);
}
or as a single Boolean check to test
或作为单个布尔检查来测试
bool isValid = Regex.IsMatch(columnValue, "^[a-z0-9 ]+$", RegexOptions.IgnoreCase);