What are best practices for structuring the $pattern string in preg_match for optimal performance?

When structuring the $pattern string in preg_match for optimal performance, it is important to keep the pattern as specific as possible to avoid unnecessary backtracking. This can be achieved by using quantifiers like +, *, or {n} judiciously, and avoiding nested quantifiers whenever possible. Additionally, using character classes [] instead of alternations | can improve performance.

$pattern = '/^[a-zA-Z0-9]+$/'; // Example of a specific and efficient pattern
$string = 'abc123';

if (preg_match($pattern, $string)) {
    echo 'Match found!';
} else {
    echo 'No match found.';
}