Alternative sans-regex solution...
Input (test.txt)
Large toolbox (metal) for sale (hammer is required) serious inquiries only.
All employees are required to attend.
Meeting scheduled for Tuesday (Formal attire required) otherwise call (or email) us.
Code
$data = file('test.txt', FILE_IGNORE_NEW_LINES|FILE_SKIP_EMPTY_LINES);
foreach ($data as $line) {
if ($p = parens_and_req($line)) {
$line = str_replace($p, "<span style='color:red;'>$p</span>", $line );
}
echo "<p>$line</p>";
}
function parens_and_req($str)
{
$k = strlen($str);
$p2 = 0;
while (( $p1 = strpos($str, '(', $p2)) !== false ) {
$p2 = strpos($str, ')', $p1);
if ($p2 === false) {
$p2 = $k-1;
}
$parens = substr($str, $p1, $p2-$p1+1);
if (strpos($parens, 'required') !== false) {
return $parens;
}
}
return false;
}
Output