How can regular expressions be optimized for better performance when extracting usernames from forum quotes in PHP?

Regular expressions can be optimized for better performance when extracting usernames from forum quotes in PHP by using the preg_match_all() function instead of preg_match(). This allows us to extract all occurrences of usernames in a given string efficiently. Additionally, using the \w+ pattern to match alphanumeric characters in usernames can improve the regex performance.

// Sample forum quote containing usernames
$forum_quote = "Hello @john_doe, how are you? - @jane_smith";

// Extract usernames using preg_match_all
preg_match_all("/@(\w+)/", $forum_quote, $matches);

// Get the extracted usernames
$usernames = $matches[1];

// Output the extracted usernames
foreach ($usernames as $username) {
    echo $username . "\n";
}