How can regex be used effectively to check for the presence of specific characters in a password?

To check for the presence of specific characters in a password using regex, you can create a regex pattern that matches the desired characters. You can then use the preg_match function in PHP to check if the password contains any of those characters.

$password = "Password123!";
$pattern = '/[!@#$%^&*]/'; // Regex pattern to match special characters

if (preg_match($pattern, $password)) {
    echo "Password contains special characters.";
} else {
    echo "Password does not contain special characters.";
}