Are there any best practices for using Regular Expressions in PHP to extract specific text from a larger string?

When using Regular Expressions in PHP to extract specific text from a larger string, it's important to use the appropriate regex pattern to match the desired text. Additionally, it's recommended to use functions like preg_match() or preg_match_all() to extract the matched text. It's also a good practice to escape special characters in the regex pattern to avoid unexpected results.

// Example code snippet to extract email addresses from a larger string using Regular Expressions in PHP
$largerString = "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Email: example@example.com. Nulla facilisi.";

$pattern = '/[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}/';
preg_match_all($pattern, $largerString, $matches);

$emailAddresses = $matches[0];

foreach ($emailAddresses as $email) {
    echo $email . "<br>";
}