How can I exclude a name from a list using a regular expression?
Data:
$subject=
"Damon Carson Stovall,
Bret Roman Mikulak,
Jack Filiberto Quirke,
Elroy Forrest Jarnagin,
Ulysses Wiley Cregger,
Brent Greg Jonah,
Bret Armand Daylong,
Ned Numbers Vandell,
Bret Alden Cutlip,
Bret Chung Markie,";
Regular expression:
// Select anyone with the first name of Bret
$pattern = "/Bret /";
My current solution:
// Loop through & Skip any line that matches Bret.
$subject_array = explode(",",$subject);
foreach($subject_array as $line)
if(!preg_match($pattern, $line))
echo "$line<br>";
Yes,you can select lines that do not have
Bretas it’s nameiis an ignore case optionmis a multiline option^depicts start of the string\bdepicts a non wordboundary(?!Bret\b)is anegative lookaheadSo
^(?!Bret\b).*$would select.*a string only if the lookahead is true…i.e if the string doesn’t start withBret!