当前位置: 动力学知识库 > 问答 > 编程问答 >

php - Compilation failed : nothing to repeat at offset 19 (preg_match)

问题描述:

I have the following simple code which check if the password contains at least two lowercases.

preg_match("/^(?=.*[a-z].*[a-z])+$/")

But this gave me the following error message:

Compilation failed : nothing to repeat at offset 19.

I can't figure where I'm wrong

Later Edit

The following code which checks if i have at least two special characters works well:

preg_match("/^(?=.*[[email protected]#$%^&*].*[[email protected]#$%^&*])[[email protected]#%^&*]+$/")

网友答案:

Try this

<?php
preg_match("/^(.*[a-z].*[a-z].*)$/", "2313123g123123u123", $result);
var_dump($result);
preg_match("/^(.*[a-z].*[a-z].*)$/", "65665656s656565", $result);
var_dump($result);
?>

result

array(2) {
    [0]=>
        string(18) "2313123g123123u123"
    [1]=>
        string(18) "2313123g123123u123"
}
array(0) {
}
网友答案:

The (?= ) defines an assertion, you can not repeat an assertion. Did you mean to use (?: )?

$data = array('ab', '123a345b', '123');

foreach ($data as $subject) {
  $found = preg_match("/^(?:.*[a-z].*[a-z])+$/", $subject, $match);
  var_dump($found, $match);
}

Output:

int(1)
array(1) {
  [0]=>
  string(2) "ab"
}
int(1)
array(1) {
  [0]=>
  string(8) "123a345b"
}
int(0)
array(0) {
}
分享给朋友:
您可能感兴趣的文章:
随机阅读: