Finding Words Not Followed by Other Words
<?php
$regex = "/\bhello\b(?!\sworld\b)/";
$valid = "hello";
$invalid = "hello world!";
if (preg_match($regex, $valid)) {
echo "Found match: '" . $valid . "'\n";
} else {
echo "No match: '" . $valid . "'\n";
}
if (preg_match($regex, $invalid)) {
echo "Found match: '" . $invalid . "'\n";
} else {
echo "No match: '" . $invalid . "'\n";
}
?>
Related examples in the same category