How can one effectively troubleshoot and debug REGEX patterns in PHP?

To effectively troubleshoot and debug REGEX patterns in PHP, use online REGEX testing tools like regex101.com to visually see how the pattern matches your input. Additionally, break down your REGEX pattern into smaller parts and test each part individually to identify where the issue lies. Utilize PHP functions like preg_match() or preg_match_all() to test your REGEX pattern within your code and output the results for debugging purposes.

$pattern = '/^([a-zA-Z0-9_-]+)@([a-zA-Z0-9_-]+)\.([a-zA-Z]{2,5})$/';
$string = 'example@email.com';

if (preg_match($pattern, $string, $matches)) {
    echo 'Valid email address';
} else {
    echo 'Invalid email address';
}