How can additional context or information improve the effectiveness of using regex for text manipulation tasks?
When using regex for text manipulation tasks, providing additional context or information can improve its effectiveness by allowing for more precise pattern matching and reducing the chance of unintended matches. This can be achieved by incorporating specific criteria, such as the expected format of the text or surrounding keywords, into the regex pattern.
// Example: Using additional context to match email addresses within a specific domain
$text = "Contact us at info@example.com or support@example.org for assistance.";
// Match email addresses ending with example.com
$pattern = '/\b[A-Za-z0-9._%+-]+@example\.com\b/';
preg_match_all($pattern, $text, $matches);
print_r($matches[0]);