Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/265.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/asp.net/37.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 正则表达式未检测到用户名文本框中的空格_C#_Asp.net_Regex - Fatal编程技术网

C# 正则表达式未检测到用户名文本框中的空格

C# 正则表达式未检测到用户名文本框中的空格,c#,asp.net,regex,C#,Asp.net,Regex,我有一个asp:textbox,其中包含一个用户名,该用户名是新用户帐户注册表单的一部分 显然,我不希望用户注册时使用空格作为名称,因此我有一个正则表达式,它应该将ASCII字符的有效条目长度保持在3到16之间,并且没有空格 但“无空格”在实践中并不起作用。它在Regex编辑器和checker中工作,但在我的aspx页面中不工作 有什么建议吗 ^([a-zA-Z0-9!@#$%^&*()-_=+;:'"|~`<>?/{}]{3,16})$|\s ^([a-zA-Z0-9!@$%^&*

我有一个asp:textbox,其中包含一个用户名,该用户名是新用户帐户注册表单的一部分

显然,我不希望用户注册时使用空格作为名称,因此我有一个正则表达式,它应该将ASCII字符的有效条目长度保持在3到16之间,并且没有空格

但“无空格”在实践中并不起作用。它在Regex编辑器和checker中工作,但在我的aspx页面中不工作

有什么建议吗

^([a-zA-Z0-9!@#$%^&*()-_=+;:'"|~`<>?/{}]{3,16})$|\s ^([a-zA-Z0-9!@$%^&*()-:'“| ~`?/{}]{3,16})$\s 非常感谢

^([a-zA-Z0-9!@#$%^&*()-_=+;:'"|~`?/{}]{3,16})$|\s ↑ ^([a-zA-Z0-9!@$%^&*()-:'“| ~`?/{}]{3,16})$\s ↑ 您当前的正则表达式说:“如果字符串由这些字符组成,并且长度为3到16个字符,则匹配字符串,如果字符串包含空白字符,则匹配长度为3到16个字符。”


因此,如果您不希望它与空格匹配,请从正则表达式中删除
|\s
(即'or'运算符和空格模式)。

一个更简单的答案是:

^([^\s]{3,16})$
这意味着“整个字符串必须由除空格以外的任何内容的三到十六个重复组成。”

这将允许使用重音字符,但无论如何,这可能是您需要接受的。

更简单

^\S{3,16}$

你似乎很难理解dtb想说什么。让我给你把正则表达式分解一下,你就会明白他在说什么:

^ - matches the beginning of the input string ( - begins a capture group, in your case useless and can be removed along with the closing ) just before the $ [ - begins a group of characters a-zA-Z0-9!@#$%^&*()-_=+;:'"|~`?/{} - defines all the characters allowed, NOTICE there is no space character so spaces will not count ] - ends the group of characters {3,16} - says that the preceding character(or group of characters in this case) must occur between 3 and 16 times ) - closes the capture group, again can be removed with the open ( $ - matches the end of the input string ^-匹配输入字符串的开头 (-开始一个捕获组,在您的情况下是无用的,可以随关闭一起删除)就在$ [-开始一组字符 a-zA-Z0-9!@#$%^&*()-=+:“| ~`?/{}-定义所有允许的字符,请注意,没有空格字符,因此空格将不计数 ]-结束字符组 {3,16}-表示前面的字符(本例中为一组字符)必须出现3到16次 )-关闭捕获组,也可以使用打开的( $-匹配输入字符串的结尾 这就是你表达错误的地方

| - says that the preceeding match expression (this is the $ which is the end of input) OR the following must be true, but not necessarily both \s - matches a space or tab anywhere in the input string |-表示前面的匹配表达式(这是作为输入结尾的$)或下面的表达式必须为真,但不一定两者都为真 \s-匹配输入字符串中任意位置的空格或制表符 因此(如果我读对了),您的正则表达式声明:

“如果字符串以ascii字符开头,在找到字符串结尾或某些空白(制表符或空格)之前长度为3到16个字符,则我匹配字符串。”

要修复此问题,请从表达式末尾删除“|\s”,只需使用以下命令:

^([a-zA-Z0-9!@#$%^&*()-_=+;:'"|~`<>?/{}]{3,16})$
^([a-zA-Z0-9!@$%^&*()-\+:“| ~`?/{}]{3,16})$这是表达式,代码在哪里?如果表达式是正确的,那么代码一定是错误的。或者可能是编译器,但不要指望它。它只是绑定到一个文本框,我需要它来检测空格,尽管您当前的正则表达式说:“我匹配字符串是否由这些字符组成,长度为3到16个字符,或者是否包含空格”好的,我是新手对不起。关于如何修改它使其与字符匹配,长度为3到16个字符,并且不包含空格,有什么建议吗?只需从正则表达式的末尾删除
\s
。好的。这里我们有了^([a-zA-Z0-9!@$%^&*()-\=+:'”“| `?/{]{3,16})$匹配长度不在3-16之间的任何ASCII字符串。因此,现在我希望它在检测到空格时停止。我应该添加什么?感谢您的解释。我没有发疯,似乎表达式的使用方式存在一些问题。我的页面仍然允许我输入未检测到的空格。但是字符串很好。我已经输入了p为了节省时间,现在就用if语句来填充空格。谢谢!