2016-07-19 186 views
0

我想检查我的字符串是否遵循这个规则?如何检查字符串是否包含某些内容?

  • 至少6个字符
  • 最多40个字符
  • 它同时包含数字和字母

我想我必须使用向前看。所以这是我到目前为止已经试过:

if (preg_match($string, "/^((?!=[0-9]{1,})(?!=[a-zA-Z]{1,})){6,40}$/")) { 
    // following 
} else { 
    // not following 
} 

但我的模式doesn't work correctly。我该如何解决它?

+0

你为什么使用负向预览? –

+0

@AnshulRai都不会工作.. *负面/积极的前瞻性* –

+0

你的'preg_match'是倒置的,正则表达式应该是第一个。这是为了验证密码吗? – chris85

回答

2

为什么要建立一个复杂的正则表达式时,你可以测试你想要的条件是什么?

function isValid($string) { 
    return strlen($string) >= 6 && 
      strlen($string) <= 40 && 
      preg_match("/\d/", $string) && 
      preg_match("/[a-zA-Z]/", $string); 
} 

// Negative test cases 
assert(!isValid("hello")); 
assert(!isValid("2shrt")); 
assert(!isValid("This one is way 2 long! This one is way 2 long! This one is way 2 long!")); 
assert(!isValid("abcdef")); 
assert(!isValid("123456")); 

// Positive test cases 
assert(isValid("abcde2")); 
assert(isValid("12345a")); 
+0

看起来不错。 thx +1 –

1

你可能不需要这么多的正则表达式,它可能会过度复杂。

我想这给一个尝试:

if(strlen($string) >= 6 && strlen($string) <= 40 && preg_match("/[a-z]/i", $string) && preg_match("/[0-9]/", $string)) { 
    //Matches rules 
else { 
    //Doesn't match 
} 
+0

您的模式与此'53test'不匹配。因为在这个例子中数字是第一位的。 –

+0

@MartinAJ修复它。 – ComputerLocus

+0

不需要第二个正则表达式中的“+”。 – smarx

1

,如果你不关心什么是在比赛中,如果你只是要检查的条件...你可以试试这个

^(?=)([0-9]|[a-zA-Z]){6,40}$

^ - starting with 
?= - followed by 
{6,40} - length 
$ - end 

编辑:关于这里的评论修改后的正则表达式:

^(?=.*[0-9])(?=.*[a-zA-Z])([a-zA-Z0-9]){6,40}$

说明

`?=` Positive Lookahead match any character followed by 0-9 OR 
`?=` Positive Lookahead match any character followed by a-z or A-Z 
`([a-zA-Z0-9])` Followed by anything in numbers or letters 
`{6,40}` length between 6,40 

EDIT 2

经过进一步评论这个表达式应该匹配:

^(?=.*[0-9])(?=.*[a-zA-Z])(.*){6,40}$类似的东西!a1234!-aquy67!

+0

你的模式匹配一​​个没有任何数字的字符串https://regex101.com/r/cX0mB4/4 –

+1

或者一个只有数字的字符串。 – smarx

+0

我相信这会拒绝“abc123 !!!”。你应该使用'.'而不是'[a-zA-Z0-9]'。 – smarx

-1
if (preg_match($string, "/^[\d\w]{6,40}$/")) { 
    // following 
} else { 
    // not following 
} 
+1

我相信“111111”会通过你的测试。 – smarx

+0

你应该添加一个解释。另外'\ w'超过'a-zA-Z'和'0-9' .. – chris85

+0

请编辑更多信息。仅限代码和“尝试这个”的答案是不鼓励的,因为它们不包含可搜索的内容,也不解释为什么有人应该“尝试这个”。 – abarisone

1

老实说,在PHP中处理这个问题的最好方法是不使用正则表达式。 PHP有一系列内置于该语言中的“ctype”方法(docs here),以允许您确定字符类型,这些方法比Regex更容易使用。不要误解我的意思Regex是一个强大而强大的工具,但对于这个问题太复杂了。即使您需要使用正则表达式来扩展我的解决方案以适应稍后的复杂模式匹配,您也可以根据您的特定需求添加它,而不必使用它来确定字符类型。

我会认真地建议您编写一系列非常小的函数/方法,然后您可以将它们组合在一起,以检查您正在验证的字符串是否通过了所有条件,如下所示(可以将该示例粘贴到文件中并在命令行运行它来查看类定义下面从脚本输出):

<?php 

/* Example written assuming that these methods are all 
* part of a Class, hence the access declarations etc. 
* If you want to write them as a function library that 
* would work fine too 
*/ 

class InputValidation { 

    const MIN_LENGTH = 6; 
    const MAX_LENGTH = 40; 

    public function isLongerThanMinLength($text) { 
     $len = strlen($text); 
     if ($len > self::MIN_LENGTH) { 
      return TRUE; 
     } else { 
      return FALSE; 
     } 
    } 

    public function isShorterThanMaxLength($text) { 
     $len = strlen($text); 
     if ($len <= self::MAX_LENGTH) { 
      return TRUE; 
     } else { 
      return FALSE; 
     } 
    } 

    public function hasAlphaAndDigitChars($text) { 
     if (ctype_alpha($text) || ctype_digit($text)) { 
      return FALSE; 
     } else { 
      if (ctype_alnum($text)) { 
       return TRUE; 
      } else { 
       return FALSE; 
      } 
     } 
    } 

    public function validInput($text) { 
     if (!$this->isLongerThanMinLength($text)) { 
      return FALSE; 
     } 
     if (!$this->isShorterThanMaxLength($text)) { 
      return FALSE; 
     } 
     if (!$this->hasAlphaAndDigitChars($text)) { 
      return FALSE; 
     } 
     return TRUE; 
    } 
} 

$inst = new InputValidation; 

$test1 = "Hello"; // too short 
$test2 = "pneumonoultramicroscopicsilicovolcanoconiosis"; // too long 
$test3 = "pneumonoultramicroscopicsil1covolcanocon"; // right length and includes at least one number 
$test4 = "123456789123456789"; // right length, but no alphabet chars 
$test5 = "HelloMyName"; // right length, but no numeric chars 
$test6 = "Hello My Name"; // right length, but has whitespace 
$test7 = "Hello^My%Name1"; // right length, but has 'special' chars 

print "\n"; 
var_dump($inst->validInput($test1)); 
print "\n"; 
var_dump($inst->validInput($test2)); 
print "\n"; 
var_dump($inst->validInput($test3)); 
print "\n"; 
var_dump($inst->validInput($test4)); 
print "\n"; 
var_dump($inst->validInput($test5)); 
print "\n"; 
var_dump($inst->validInput($test6)); 
print "\n"; 
var_dump($inst->validInput($test7)); 
print "\n"; 
print "\n"; 

exit(); 
?> 

这样,您就可以使用不同的验证方法,通过放在一起,以增加此验证的复杂性,或创建新的验证以不同方式使用现有规则,或通过添加新的简短方法轻松添加新规则。

+0

Nice ... thx +1 –

+0

感谢@MartinAJ - 我仍然使用PHP,而不是我喜欢的,但是当我这样做时,我采用了很多来自其他编程语言的习语,以使我的代码更具可读性和可重用性,所以它更容易组合在一起。我认为一般而言,PHP代码可以从写得更像习惯Ruby,真正受益,使用小巧简洁的函数,这些函数在合成或链接中被调用,特别是如果您有一个良好的IDE和/或调试器来遵循这些值从功能到功能。 –

相关问题