How can regular expressions be effectively used in PHP for parsing and processing complex data?

Regular expressions can be effectively used in PHP for parsing and processing complex data by allowing you to define patterns to match specific strings within the data. This can be useful for tasks such as extracting information from text, validating input, or replacing certain patterns with other strings.

$data = "Hello, my email is john.doe@example.com and my phone number is 555-123-4567.";
$email_pattern = '/[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,}/';
$phone_pattern = '/\d{3}-\d{3}-\d{4}/';

preg_match($email_pattern, $data, $email_matches);
preg_match($phone_pattern, $data, $phone_matches);

$email = $email_matches[0];
$phone = $phone_matches[0];

echo "Email: $email\n";
echo "Phone: $phone\n";