用于过滤不需要的用户名的正则表达式

发布于 2024-09-15 10:54:59 字数 463 浏览 5 评论 0原文

在我正在开发的网站上,我要求用户名不能以 _ 开头,

因此不应允许这些:

  • SU_Coolguy
  • MR_Nobody
  • my_Pony

但这些应该没问题:

  • __SU_Coolguy
  • MRS_Nobody
  • YourPony

在我使用的框架中,我只能验证匹配的正则表达式,而不能验证不匹配的情况。到目前为止我已经想出了这个:

"/^([^A-Za-z0-9]{2}\_|[A-Za-z0-9]{3,27})/"

这适用于大多数项目,但在“__SU_Coolguy”上失败。

对此正则表达式的任何帮助将不胜感激。 :)

On a site I am working on I have a requirement that usernames not start with <alpha><alpha>_

So these should not be allowed:

  • SU_Coolguy
  • MR_Nobody
  • my_Pony

But these should be ok:

  • __SU_Coolguy
  • MRS_Nobody
  • YourPony

In the framework I am using, I am only able to validate against matching regular expression, not non-matching. So far I have come up with this:

"/^([^A-Za-z0-9]{2}\_|[A-Za-z0-9]{3,27})/"

This works for most items, but fails on "__SU_Coolguy".

Any help on this regex would be highly appreciated. :)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(4

我不是你的备胎 2024-09-22 10:54:59

这使用 负向预测

^(?![A-Za- z]{2}_)[A-Za-z0-9_]{3,27}$

让我们分解一下:

Assert position at the beginning of a line (at beginning of the string or after a line break character) «^»
Assert that it is impossible to match the regex below starting at this position (negative lookahead) «(?![A-Za-z]{2}_)»
   Match a single character present in the list below «[A-Za-z]{2}»
      Exactly 2 times «{2}»
      A character in the range between “A” and “Z” «A-Z»
      A character in the range between “a” and “z” «a-z»
   Match the character “_” literally «_»
Match a single character present in the list below «[A-Za-z0-9_]{3,27}»
   Between 3 and 27 times, as many times as possible, giving back as needed (greedy) «{3,27}»
   A character in the range between “A” and “Z” «A-Z»
   A character in the range between “a” and “z” «a-z»
   A character in the range between “0” and “9” «0-9»
   The character “_” «_»
Assert position at the end of a line (at the end of the string or before a line break character) «$»

This uses a negative lookahead:

^(?![A-Za-z]{2}_)[A-Za-z0-9_]{3,27}$

Lets break it down:

Assert position at the beginning of a line (at beginning of the string or after a line break character) «^»
Assert that it is impossible to match the regex below starting at this position (negative lookahead) «(?![A-Za-z]{2}_)»
   Match a single character present in the list below «[A-Za-z]{2}»
      Exactly 2 times «{2}»
      A character in the range between “A” and “Z” «A-Z»
      A character in the range between “a” and “z” «a-z»
   Match the character “_” literally «_»
Match a single character present in the list below «[A-Za-z0-9_]{3,27}»
   Between 3 and 27 times, as many times as possible, giving back as needed (greedy) «{3,27}»
   A character in the range between “A” and “Z” «A-Z»
   A character in the range between “a” and “z” «a-z»
   A character in the range between “0” and “9” «0-9»
   The character “_” «_»
Assert position at the end of a line (at the end of the string or before a line break character) «$»
国粹 2024-09-22 10:54:59

您的正则表达式将为 /^(?![a-zA-Z0-9]{2}_)/。它的意思是“以不是{两个字母数字字符和一个下划线}开头”。

Your regex would be /^(?![a-zA-Z0-9]{2}_)/. It means "start with not {two alphanumeric characters and an underscore}".

你与清晨阳光 2024-09-22 10:54:59

只需输入一个否定断言,如下所示:

/^([^A-Za-z0-9]{2}(?!\_)|[A-Za-z0-9]{3,27})/
                   ^^--Notice the assertion.

这是一个完整的测试用例:

<?php
$names = array('SU_Coolguy','MR_Nobody','my_Pony','__SU_Coolguy','MRS_Nobody','YourPony');

foreach($names as $name){
        echo "$name => ";
        if(preg_match('/^([^A-Za-z0-9]{2}(?!\_)|[A-Za-z0-9]{3,27})/',$name)) {
                echo 'ok';
        }else{
                echo 'fail';
        }
        echo "\n";
}
?>

输出如下:

SU_Coolguy => fail
MR_Nobody => fail
my_Pony => fail
__SU_Coolguy => ok
MRS_Nobody => ok
YourPony => ok

Just put in a negative assertion, like this:

/^([^A-Za-z0-9]{2}(?!\_)|[A-Za-z0-9]{3,27})/
                   ^^--Notice the assertion.

Here is a full test case:

<?php
$names = array('SU_Coolguy','MR_Nobody','my_Pony','__SU_Coolguy','MRS_Nobody','YourPony');

foreach($names as $name){
        echo "$name => ";
        if(preg_match('/^([^A-Za-z0-9]{2}(?!\_)|[A-Za-z0-9]{3,27})/',$name)) {
                echo 'ok';
        }else{
                echo 'fail';
        }
        echo "\n";
}
?>

which outputs this:

SU_Coolguy => fail
MR_Nobody => fail
my_Pony => fail
__SU_Coolguy => ok
MRS_Nobody => ok
YourPony => ok
美人骨 2024-09-22 10:54:59

在这种情况下,一个简单的方法是将其分成不同的情况:用户名要么以非字母开头,要么以字母和非字母开头,要么以两个字母和非下划线开头,大致如下:

/^([^A-Za-z]|[A-Za-z][^A-Za-z]|[A-Za-z][A-Za-z][^_])/

A simple way in this case is just to break it into cases: the username either starts with a non-alpha, or an alpha and a non-alpha, or two alphas and non-underscore, so roughly like:

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