允许数字、字母和空格的用户名正则表达式

发布于 2024-09-12 18:52:21 字数 277 浏览 3 评论 0原文

我正在寻找一些可用于检查有效用户名的正则表达式代码。

我希望用户名包含字母(大写和小写)、数字、空格、下划线、破折号和点,但用户名必须以字母或数字开头和结尾。

理想情况下,它也不应该允许上面列出的任何特殊字符连续重复一次以上,即它们可以有任意数量的空格/点/破折号/下划线,但必须至少有一个数字或他们之间的信。

我也有兴趣了解您是否认为这对于用户名来说是一个好的系统?我寻找了一些可以做到这一点的正则表达式,但它们似乎都不允许空格,并且我希望用户名中包含一些空格。

谢谢 :)

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

混吃等死 2024-09-19 18:52:21

所以看起来您希望您的用户名有一个“单词”部分(字母或数字序列),并散布一些“分隔符”部分。

正则表达式看起来像这样:

^[a-z0-9]+(?:[ _.-][a-z0-9]+)*$

这是一个示意性细分:

           _____sep-word…____
          /                  \
^[a-z0-9]+(?:[ _.-][a-z0-9]+)*$             i.e. "word ( sep word )*"
|\_______/   \____/\_______/  |
| "word"     "sep"   "word"   |
|                             |
from beginning of string...   till the end of string

所以本质上我们想要匹配诸如 wordword-sep-wordword-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:

^[a-z0-9]+(?:[ _.-][a-z0-9]+)*$

Here's a schematic breakdown:

           _____sep-word…____
          /                  \
^[a-z0-9]+(?:[ _.-][a-z0-9]+)*$             i.e. "word ( sep word )*"
|\_______/   \____/\_______/  |
| "word"     "sep"   "word"   |
|                             |
from beginning of string...   till the end of string

So essentially we want to match things like word, word-sep-word, word-sep-word-sep-word, etc.

  • There will be no consecutive sep without a word in between
  • The first and last char will always be part of a word (i.e. not a sep 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

仙女 2024-09-19 18:52:21

尽管我确信有人很快就会发布 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:

it should also not allow for any of
the special characters listed above to
be repeated more than once in
succession

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.

半边脸i 2024-09-19 18:52:21

您不必对所有事情都使用正则表达式。我发现像“没有两个连续字符”这样的要求通常会使正则表达式变得非常难看,所以最好用一个简单的程序循环来做到这一点。

我只是使用类似 ^[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:

  • not allowed to contain your first or last name.
  • no more than two consecutive digits.

and so forth.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文