Lookahead in PHP Regex
Sometimes you need to solve a problem of this type: find
the string 'aaa'
and replace it with '!'
,
but only if after 'aaa'
there is
'x'
, and the 'x'
itself
should not be replaced. If we try to solve the problem
'in a straightforward way', we will not succeed:
<?php
preg_replace('#aaax#', '!', 'aaax'); // will be '!', but should be '!x'
?>
To solve the problem, we need a way to say
that 'x'
should not be replaced. This is done
using special brackets (?= )
,
which only look ahead but do not consume the characters.
These brackets are called positive lookahead
forward. Positive - because 'x'
(in our case) must be present - only then
the replacement will occur.
Let's apply these brackets to solve our task:
<?php
preg_replace('#aaa(?=x)#', '!', 'aaax'); // returns '!x'
?>
There is also negative lookahead forward
- (?! )
- it, on the contrary, says that
something must not be present. In the next example
the replacement will occur only if after 'aaa'
there is no 'x'
:
<?php
preg_replace('#aaa(?!x)#', '!', 'aaab'); // returns '!b'
?>
Given a string containing function names:
<?php
$str = 'func1() func2() func3()';
?>
Get an array of function names from the string.
Given a string with a tag:
<?php
$str = '<a href="" class="eee" id="zzz">';
?>
Get an array of attribute names of this tag.