Are there any best practices for using regular expressions in PHP to extract specific elements from a string?

When using regular expressions in PHP to extract specific elements from a string, it is important to define a clear pattern that matches the desired elements. It is recommended to use the preg_match() function to search for the pattern in the string and extract the matched elements. Additionally, using capturing groups in the regular expression can help isolate the specific elements that need to be extracted.

$string = "Hello, my email is john.doe@example.com";
$pattern = '/[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}/';
preg_match($pattern, $string, $matches);
$email = $matches[0];
echo $email;