Look ahead and look behind can be very useful to match a pattern which is followed, not followed, preceded or preceded by a certain pattern. e.g. We can match a String which is not followed by another specific string.
Php preg_match negative look ahead example
Look for hello which is not followed by world. We’ll use the pattern (?! world)
for negative lookahead.
Case 1
hello is followed by world. So negative look ahead should fail.
<?php if (preg_match('/hello(?! world)/i', "hello world", $matches, PREG_OFFSET_CAPTURE)) { echo "matched string:\n{$matches[0][0]} (at offset {$matches[0][1]})\n"; } else { echo "No match\n"; } ?>
No match
Case 2
hello is not followed by world. So negative look ahead should succeed.
<?php if (preg_match('/hello(?! world)/i', "hello something else", $matches, PREG_OFFSET_CAPTURE)) { echo "matched string:\n{$matches[0][0]} (at offset {$matches[0][1]})\n"; } else { echo "No match\n"; } ?>
matched string: hello (at offset 0)
Php preg_replace positive look ahead example
Replace hello (with NEWVAL) which is followed by world. We’ll use the pattern (?= world)
for positive lookahead.
<?php $str = "hello world"; $newstr = preg_replace('/hello(?= world)/i', 'NEWVAL', $str, -1, $count); if ($count > 0) { echo "newstr after $count replacement(s):\n$newstr\n"; } else { echo "No replacement\n"; } ?>
newstr after 1 replacement(s): NEWVAL world
<?php $str = "hello something else"; $newstr = preg_replace('/hello(?= world)/i', 'NEWVAL', $str, -1, $count); if ($count > 0) { echo "newstr after $count replacement(s):\n$newstr\n"; } else { echo "No replacement\n"; } ?>
No replacement
Php preg_match negative look behind example
Look for hello which is not preceded by any non alpha numeric character. We’ll use the pattern (?<![a-z0-9])
for negative lookbehind.
Case 1
hello is not preceded by anything
<?php if (preg_match('/(?<![a-z0-9])hello/i', "hello world", $matches, PREG_OFFSET_CAPTURE)) { echo "matched string:\n{$matches[0][0]} (at offset {$matches[0][1]})\n"; } else { echo "No match\n"; } ?>
matched string: hello (at offset 0)
Case 2
hello is preceded by alphanumeric character a
<?php if (preg_match('/(?<![a-z0-9])hello/i', "ahello", $matches, PREG_OFFSET_CAPTURE)) { echo "matched string:\n{$matches[0][0]} (at offset {$matches[0][1]})\n"; } else { echo "No match\n"; } ?>
No match
Php preg_replace positive look behind example
Replace world (with NEWVAL) when it is preceded by hello. We’ll use the pattern (?<=hello )
for positive lookbehind.
<?php $str = "hello world"; $newstr = preg_replace('/(?<=hello )world/i', 'NEWVAL', $str, -1, $count); if ($count > 0) { echo "newstr after $count replacement(s):\n$newstr\n"; } else { echo "No replacement\n"; } ?>
newstr after 1 replacement(s): hello NEWVAL
<?php $str = "something world"; $newstr = preg_replace('/(?<=hello )world/i', 'NEWVAL', $str, -1, $count); if ($count > 0) { echo "newstr after $count replacement(s):\n$newstr\n"; } else { echo "No replacement\n"; } ?>
No replacement
Few points to note
-
The flag
i
after / is used for ignoring case. - The lookahead or lookbehind does not become part of pattern match ($matches in preg_match) or replaced content (in preg_replace).