What are some best practices for constructing regular expressions in PHP to ensure accurate and efficient pattern matching?

When constructing regular expressions in PHP, it is important to follow best practices to ensure accurate and efficient pattern matching. One key tip is to use anchors (^ and $) to match the beginning and end of a string, respectively. Additionally, using quantifiers (*, +, ?) sparingly and efficiently can help improve performance. Lastly, testing and debugging your regular expressions thoroughly using tools like regex101.com can help catch any errors or inefficiencies.

// Example of constructing a regular expression in PHP with best practices
$pattern = '/^([a-zA-Z0-9._%+-]+)@([a-zA-Z0-9.-]+)\.([a-zA-Z]{2,})$/';
$email = "example@email.com";

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