繁体   English   中英

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

[英]regex not detecting space in username textbox

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

显然,我不希望用户使用空格作为名称进行注册,因此我拥有此正则表达式,该表达式应将有效输入的ASCII字符的长度保持在3到16之间,并且没有空格。

但实际上没有空格是行不通的。 它适用于Regex编辑器和检查器,但不适用于我的aspx页面。

有什么建议么?

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

非常感谢

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

您当前的正则表达式说: “如果字符串由这些字符组成并且长度为3至16个字符, 或者如果包含空格字符,匹配字符串。”

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

您似乎无法理解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

这是您的表情出现问题的地方...

| - 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

所以(如果我没看错的话)您的正则表达式指出:

“如果字符串以ascii字符开头并且长度为3到16个字符,那么它会在找到字符串的末尾或某些空格(制表符或空格)之前与之匹配。”

要解决此问题,请从表达式末尾删除“ | \\ s”,然后使用以下命令:

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

一个简单的答案是:

^([^\s]{3,16})$

这表示“整个字符串必须包含3到16个除空白以外的任何重复。”

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

更简单的是

^\S{3,16}$

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM