Preg_match para todos los caracteres especiales, comprobación de contraseña

Ok so I am writing a password checker for our password policy which requires 3 of the 4 major classifications. Where I'm having problems with is the special character match.

Esto es lo que tengo hasta ahora:

private function PasswordRequirements($ComplexityCount) {
    $Count = 0;
    if(preg_match("/\d/", $this->PostedData['password']) > 0) {
        $Count++;
    }
    if(preg_match("/[A-Z]/", $this->PostedData['password']) > 0) {
        $Count++;
    }
    if(preg_match("/[a-z]/", $this->PostedData['password']) > 0) {
        $Count++;
    }
    // This is where I need help
    if(preg_match("/[~`!@#$%^&*()_-+=\[\]{}\|\\:;\"\'<,>.]/", $this->PostedData['password']) > 0) {
        $Count++;
    }

    if($Count >= $ComplexityCount) {
        return true;
    } else {
        return false;
    }
}

So basically what I'm doing is checking the string for each case, numbers, uppercase, lowercase, and special characters. We don't have any restrictions on any special character and I also need unicode characters. Does the \W work in this case or would that also include numbers again? I can't find great documentation on \W so I'm unclear on this part.

Does anyone know of a easy regexp that would cover all special characters and unicode characters that does not include numbers and letters?

Anyone is free to use this as I think more than a few people have been looking for this.

preguntado el 08 de noviembre de 11 a las 16:11

Here's a great page that explain some of your speculations: regular-expressions.info/reference.html -

3 Respuestas

This pattern would allow all characters that's not a digit or a-Z.

[^\da-zA-Z]

En cuanto a \W it's a negated \w, que es lo mismo que [A-Za-z0-9_]. Thus will \W be all characters that's not an english letter, digit or an underscore.

As I mentioned as a comment este es un gran recurso for learning regex. And here's a good site to probar la expresión regular.

Respondido el 11 de diciembre de 15 a las 09:12

I'm slapping myself up the head. Thank you so much I didn't even think of doing the reverse of the previous checks. And thank you for that resource that will come in handy. - Jeff - JeffBaumgardt

In case you want to match on special characters

preg_match('/[\'\/~`\!@#\$%\^&\*\(\)_\-\+=\{\}\[\]\|;:"\<\>,\.\?\\\]/', $input)

respondido 10 mar '12, 05:03

You can use the POSIX character class [[:punct:]] for the 'special' characters:

<?php
$regex = '[[:punct:]]';

if (preg_match('/'.$regex.'/', 'somepas$', $matches)) {
    print_r($matches);
}
?>

da:

Array
(
    [0] => $
)

respondido 08 nov., 11:20

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.