Php look ahead and look behind regex examples

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-regex-look-behind-look-ahead

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
Env: PHP version 5.5.9 (Linux)

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)
Env: PHP version 5.5.9 (Linux)

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
Env: PHP version 5.5.9 (Linux)

<?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
Env: PHP version 5.5.9 (Linux)

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)
Env: PHP version 5.5.9 (Linux)

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
Env: PHP version 5.5.9 (Linux)

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
Env: PHP version 5.5.9 (Linux)

<?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
Env: PHP version 5.5.9 (Linux)

Few points to note

  1. The flag i after / is used for ignoring case.
  2. The lookahead or lookbehind does not become part of pattern match ($matches in preg_match) or replaced content (in preg_replace).
Share this article: share on Google+ share on facebook share on linkedin tweet this submit to reddit

Comments

Click here to write/view comments