What are some best practices for creating and using patterns in preg_match_all to extract specific data in PHP?

When using preg_match_all to extract specific data in PHP, it's important to create a well-defined pattern that matches the desired data accurately. Best practices include using capturing groups to isolate the data of interest, using quantifiers to match repeated patterns, and escaping special characters to prevent unexpected behavior.

// Example code snippet
$data = "Hello, my email is example@email.com and my phone number is 123-456-7890.";
$pattern = '/(\b[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Z|a-z]{2,}\b)|(\b\d{3}-\d{3}-\d{4}\b)/';
preg_match_all($pattern, $data, $matches);

print_r($matches);