允许数字、字母和空格的用户名正则表达式
我正在寻找一些可用于检查有效用户名的正则表达式代码。
我希望用户名包含字母(大写和小写)、数字、空格、下划线、破折号和点,但用户名必须以字母或数字开头和结尾。
理想情况下,它也不应该允许上面列出的任何特殊字符连续重复一次以上,即它们可以有任意数量的空格/点/破折号/下划线,但必须至少有一个数字或他们之间的信。
我也有兴趣了解您是否认为这对于用户名来说是一个好的系统?我寻找了一些可以做到这一点的正则表达式,但它们似乎都不允许空格,并且我希望用户名中包含一些空格。
谢谢 :)
I'm looking for some regex code that I can use to check for a valid username.
I would like for the username to have letters (both upper case and lower case), numbers, spaces, underscores, dashes and dots, but the username must start and end with either a letter or number.
Ideally, it should also not allow for any of the special characters listed above to be repeated more than once in succession, i.e. they can have as many spaces/dots/dashes/underscores as they want, but there must be at least one number or letter between them.
I'm also interested to find out if you think this is a good system for a username? I've had a look for some regex that could do this, but none of them seem to allow spaces, and I would like for the usernames to have some spaces in them.
Thank you :)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
所以看起来您希望您的用户名有一个“单词”部分(字母或数字序列),并散布一些“分隔符”部分。
正则表达式看起来像这样:
这是一个示意性细分:
所以本质上我们想要匹配诸如
word
、word-sep-word
、word-sep- word-sep-word
等。word
的连续sep
的一部分>word
(即不是sep
字符)请注意,对于
[ _.-]
,-
位于最后,因此它不是范围定义元字符。(?:…)
是所谓的非捕获组。我们需要括号来对重复进行分组(即(…)*
),但由于我们不需要捕获,所以我们可以使用(?:…)*
反而。要允许大写/各种 Unicode 字母等,只需扩展字符类/根据需要使用更多标志。
参考文献
So it looks like you want your username to have a "word" part (sequence of letters or numbers), interspersed with some "separator" part.
The regex will look something like this:
Here's a schematic breakdown:
So essentially we want to match things like
word
,word-sep-word
,word-sep-word-sep-word
, etc.sep
without aword
in betweenword
(i.e. not asep
char)Note that for
[ _.-]
,-
is last so that it's not a range definition metacharacter. The(?:…)
is what is called a non-capturing group. We need the brackets for grouping for the repetition (i.e.(…)*
), but since we don't need the capture, we can use(?:…)*
instead.To allow uppercase/various Unicode letters etc, just expand the character class/use more flags as necessary.
References
尽管我确信有人很快就会发布 100 万行正则表达式来完全满足您的要求,但我认为在这种情况下正则表达式不是一个好的解决方案。
你为什么不写一个好的老式解析器呢?花费的时间与编写执行您提到的所有操作的正则表达式一样长,但维护和阅读会更容易。
特别是,这是棘手的部分:
或者,您始终可以将两者混合起来 用于其他检查的正则表达式 (
[a-zA-Z0-9][a-zA-Z0-9 _-\.]*[a-zA-Z0-9]
) 和非-用于无重复要求的正则表达式方法。Although I'm sure someone will shortly post a 1 million lines regex to do exactly what you want, I don't think in this case a regex is a good solution.
Why don't you write a good old fashioned parser? It will take about as long as writing the regex that does everything you mentioned, but it's going to be much easier to maintain and read.
In particular, this is the tricky part:
Alternatively you can always do a hybrid of the two. A regex for the other checks (
[a-zA-Z0-9][a-zA-Z0-9 _-\.]*[a-zA-Z0-9]
) and a non-regex method for the no-repeat requirement.您不必对所有事情都使用正则表达式。我发现像“没有两个连续字符”这样的要求通常会使正则表达式变得非常难看,所以最好用一个简单的程序循环来做到这一点。
我只是使用类似
^[A-Za-z0-9][A-Za-z0-9 \.\-_]*[A-Za-z0-9]$
(或者类似::alnum::
(如果您的正则表达式引擎更高级),然后只需检查循环中的每个字符以确保下一个字符不相同。通过按程序执行此操作,您可以在某个时候检查您可能想要的所有其他规则,而无需诉诸我所说的“正则表达式体操”,例如:
等等。
You don't have to use a regex for everything. I find that requirements like the "no two consecutive characters" usually make the regexes so ugly that it's better to do that bit with a simple procedural loop.
I'd just use something like
^[A-Za-z0-9][A-Za-z0-9 \.\-_]*[A-Za-z0-9]$
(or the equivalents like::alnum::
if your regex engine is more advanced) and then just check every character in a loop to make sure the next character isn't the same.By doing it procedurally, you can check all the other rules you're likely to want at some point without resorting to what I call "regex gymnastics", things like:
and so forth.