InfoHeap
Tech
Navigation
  • Home
  • Tutorials
    • CSS tutorials & examples
    • CSS properties
    • Javascript cookbook
    • Linux/Unix Command Line
    • Mac
    • PHP
      • PHP functions online
      • PHP regex
    • WordPress
  • Online Tools
    • Text utilities
    • Online Lint Tools
search

PHP regex

  • preg_match online
  • preg_match_all online
  • preg_replace online
  • Regex OR (alternation)
  • beginning and end of string
  • look ahead and look behind
  • preg_match - greedy and lazy
  • preg_replace - optional char group
  • preg_replace - optional one char
  • preg_replace back reference (in replacement text)
  • preg_replace – back reference within pattern
  • preg_replace – dot all
  • regex delimiters
  • regex shorthand - digit (\w)
  • regex shorthand - whitespac (\s)
  • regex shorthand - word boundary (\b)
  • regex shorthand - word character (\w)
  • remove non printable chars
 
  • Home
  • > Tutorials
  • > PHP
  • > PHP Regex

Php look ahead and look behind regex examples

By admin on Jan 4, 2016

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";
}
?>
try it online
No match
Env: PHP version 7.4.33 (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";
}
?>
try it online
matched string:
hello (at offset 0)
Env: PHP version 7.4.33 (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";
}
?>
try it online
newstr after 1 replacement(s):
NEWVAL world
Env: PHP version 7.4.33 (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";
}
?>
try it online
No replacement
Env: PHP version 7.4.33 (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";
}
?>
try it online
matched string:
hello (at offset 0)
Env: PHP version 7.4.33 (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";
}
?>
try it online
No match
Env: PHP version 7.4.33 (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";
}
?>
try it online
newstr after 1 replacement(s):
hello NEWVAL
Env: PHP version 7.4.33 (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";
}
?>
try it online
No replacement
Env: PHP version 7.4.33 (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).

Suggested posts:

  1. Bash – local and global variables
  2. PHP regex – whitespace shorthand (\s) regex examples
  3. PHP regex – match any digit
  4. Bash – how to compare file timestamps
  5. PHP – convert dos newline to unix format
  6. Css source map example using gulp-sourcemaps
  7. Css :first-of-type selector – first child element of type
  8. Create circle and oval in CSS
Share this article: share on facebook share on linkedin tweet this submit to reddit
Posted in Tutorials | Tagged PHP, PHP Regex, preg_match, preg_replace, Regular Expression, Tutorials
  • Browse content
  • Article Topics
  • Article archives
  • Contact Us
Popular Topics: Android Development | AngularJS | Apache | AWS and EC2 | Bash shell scripting | Chrome developer tools | Company results | CSS | CSS cookbook | CSS properties | CSS Pseudo Classes | CSS selectors | CSS3 | CSS3 flexbox | Devops | Git | HTML | HTML5 | Java | Javascript | Javascript cookbook | Javascript DOM | jQuery | Kubernetes | Linux | Linux/Unix Command Line | Mac | Mac Command Line | Mysql | Networking | Node.js | Online Tools | PHP | PHP cookbook | PHP Regex | Python | Python array | Python cookbook | SEO | Site Performance | SSH | Ubuntu Linux | Web Development | Webmaster | Wordpress | Wordpress customization | Wordpress How To | Wordpress Mysql Queries | InfoHeap Money

Copyright © 2025 InfoHeap.

Powered by WordPress