我想检查遵循此规则是否符合我的字符串?
我想我必须使用前瞻。所以这是我到目前为止所尝试的:
if ( preg_match( $string, "/^((?!=[0-9]{1,})(?!=[a-zA-Z]{1,})){6,40}$/") ) {
// following
} else {
// not following
}
但是我的模式doesn't work correctly。我该如何解决?
答案 0 :(得分: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"));
答案 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
}
答案 2 :(得分: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
编辑2
进一步评论后,这个正则表达式应该匹配:
^(?=.*[0-9])(?=.*[a-zA-Z])(.*){6,40}$
类似!a1234!
或-aquy67!
答案 3 :(得分:1)
老实说,在PHP中处理此问题的最佳方法是不使用正则表达式。 PHP在语言中内置了一系列“ctype”方法(docs here),以便您确定字符类型,这些方法比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();
?>
通过这种方式,您可以使用不同的验证方法来增加此验证的复杂性,或者通过以不同方式组合现有规则来创建新验证,或者通过添加新的简短方法轻松添加新规则。
答案 4 :(得分:-1)
if ( preg_match( $string, "/^[\d\w]{6,40}$/") ) {
// following
} else {
// not following
}