Are there any best practices to consider when using regular expressions in PHP to extract content from files?

When using regular expressions in PHP to extract content from files, it's important to consider best practices to ensure efficient and accurate extraction. One key practice is to use the appropriate regular expression pattern that matches the content you want to extract. Additionally, it's important to handle error cases and edge cases in your regular expression pattern to avoid unexpected results.

// Example code snippet to extract email addresses from a file using regular expressions
$file_content = file_get_contents('example.txt');
$pattern = '/[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}/';
preg_match_all($pattern, $file_content, $matches);

$email_addresses = $matches[0];

foreach ($email_addresses as $email) {
    echo $email . "\n";
}