Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/regex/18.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
Regex 用于筛选不需要的用户名的正则表达式_Regex_Pcre - Fatal编程技术网

Regex 用于筛选不需要的用户名的正则表达式

Regex 用于筛选不需要的用户名的正则表达式,regex,pcre,Regex,Pcre,在我正在工作的网站上,我有一个要求,用户名不能以开头 因此,这些都是不允许的: 苏酷 吴先生 我的小马 但这些应该没问题: __苏酷 胡女士 你的小马 在我使用的框架中,我只能根据匹配的正则表达式进行验证,而不能根据不匹配的正则表达式进行验证。到目前为止,我已经得出了以下结论: 这适用于大多数物品,但在“酷家伙”上失败 在此正则表达式上的任何帮助都将不胜感激。:) 您的正则表达式应该是/^(?![a-zA-Z0-9]{2}}}/。它的意思是“不以{两个字母数字字符和一个下划线}开头”。

在我正在工作的网站上,我有一个要求,用户名不能以
开头

因此,这些都是不允许的:

  • 苏酷
  • 吴先生
  • 我的小马
但这些应该没问题:

  • __苏酷
  • 胡女士
  • 你的小马
在我使用的框架中,我只能根据匹配的正则表达式进行验证,而不能根据不匹配的正则表达式进行验证。到目前为止,我已经得出了以下结论:

这适用于大多数物品,但在“酷家伙”上失败


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

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

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

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

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

在这种情况下,一种简单的方法就是将其分为几类:用户名要么以非alpha开头,要么以alpha和非alpha开头,要么以两个alpha和非下划线开头,大致如下所示:

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

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

/^([^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";
}
?>

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

/^([^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";
}
?>
这使用了:

^(?![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) «$»
这使用了:

^(?![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) «$»


如果你需要一个PHP正则表达式,它不是语言不可知的。@Peter Boughton:事实上,因为他想要的只是正则表达式,它也与PHP无关。PHP使用PCRE,因此重新标记为
PCRE regex
。这很公平,不过我还是保留
PHP
标记作为一般规则,因为在某些情况下,非regex解决方案可能更好,所以
PHP
标记使人们更容易看到它。@Peter Boughton:在这种情况下,他的(未知)框架只允许他使用正则表达式,所以对于给定的数据,非正则表达式的解决方案是不可能的。好的一点-我读错了这一点。如果你需要一个PHP正则表达式,它不是语言不可知的。@Peter Boughton:事实上,因为他想要的只是正则表达式,它也不是关于PHP的。PHP使用PCRE,因此重新标记为
PCRE regex
。这很公平,不过我还是保留
PHP
标记作为一般规则,因为在某些情况下,非regex解决方案可能更好,所以
PHP
标记使人们更容易看到它。@Peter Boughton:在这种情况下,他的(未知)框架只允许他使用正则表达式,所以对于给定的数据,非正则表达式的解决方案是不可能的。好的一点-我读错了这一点。我正要发布同样的东西,嗯,除了忽略
A-Z
并在末尾添加
i
标志外。+1并接受我的工作,以完全满足我的需要,而不在末尾添加额外的垃圾。但这不会检查字符串的其余部分是字母数字还是数字。bancer,这不是一项要求。但是在结束斜杠之前推压
\w+$
也可以。我正要发布同样的内容,除了忽略
A-Z
并在末尾添加
i
标志外。+1并接受我的工作,以完全满足我的需要,而不在末尾添加额外的垃圾。但这不会检查字符串的其余部分是字母数字还是数字。bancer,这不是一项要求。但是,在结束斜杠之前推压
\w+$
可以做到这一点。是否每次都需要写入“介于”之间的字符?使其可读性降低,这与目的正好相反。此外,如果您在注释前面加上
#
前缀,则会停止颜色编码(以及启用
x
标记时的有效正则表达式语法)。抱歉,这是由RegexBuddy生成的(我懒得自己键入解释),是否每次都需要写入“介于之间的字符”?使其可读性降低,这与目的正好相反。另外,如果您在注释前面加上
#
前缀,则会停止颜色编码(以及启用
x
标记时的有效正则表达式语法)。抱歉,这是由RegexBuddy生成的(懒得自己键入解释)