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

时间:2010-08-18 17:06:53

标签: regex pcre

在我正在处理的网站上,我要求用户名不以<alpha><alpha>_开头

所以不应该允许这些:

  • SU_Coolguy
  • MR_Nobody
  • my_Pony

但这些应该没问题:

  • __ SU_Coolguy
  • MRS_Nobody
  • YourPony

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

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

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

对此正则表达式的任何帮助都将受到高度赞赏。 :)

4 个答案:

答案 0 :(得分:3)

这使用negative lookahead

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

答案 1 :(得分:2)

你的正则表达式是/^(?![a-zA-Z0-9]{2}_)/。这意味着“从开始 {两个字母数字字符和一个下划线}”。

答案 2 :(得分:1)

只是提出一个否定的断言,就像这样:

/^([^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

答案 3 :(得分:0)

在这种情况下,一种简单的方法就是将其分解为案例:用户名以非alpha开头,或者以alpha开头,非alpha开头,或者两个alphas和非下划线,大致如下:

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