0
0
PHPprogramming~20 mins

Lookahead and lookbehind in PHP - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Lookahead and Lookbehind Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output of positive lookahead regex in PHP
What is the output of this PHP code snippet?
$text = "apple pie and apple tart";
$pattern = '/apple(?= pie)/';
preg_match_all($pattern, $text, $matches);
print_r($matches[0]);
PHP
$text = "apple pie and apple tart";
$pattern = '/apple(?= pie)/';
preg_match_all($pattern, $text, $matches);
print_r($matches[0]);
AArray ( [0] => apple [1] => apple )
BArray ( [0] => pie )
CArray ( [0] => apple )
DArray ( )
Attempts:
2 left
💡 Hint
Lookahead checks what comes after the word without including it.
Predict Output
intermediate
2:00remaining
Output of negative lookbehind regex in PHP
What will this PHP code output?
$text = "cat bat rat mat";
$pattern = '/(?preg_match_all($pattern, $text, $matches);
print_r($matches[0]);
PHP
$text = "cat bat rat mat";
$pattern = '/(?<!b)at/';
preg_match_all($pattern, $text, $matches);
print_r($matches[0]);
AArray ( [0] => at [1] => at [2] => at )
BArray ( [0] => bat )
CArray ( [0] => at [1] => at )
DArray ( )
Attempts:
2 left
💡 Hint
Negative lookbehind excludes matches preceded by 'b'.
🔧 Debug
advanced
2:00remaining
Identify the error in this lookbehind regex
This PHP code throws an error. What is the cause?
$pattern = '/(?<=\d+)abc/';
$text = '12abc';
preg_match($pattern, $text, $matches);
PHP
$pattern = '/(?<=\d+)abc/';
$text = '12abc';
preg_match($pattern, $text, $matches);
ALookbehind pattern has variable length which is not supported
BMissing delimiter in regex pattern
CUnescaped backslash in pattern
Dpreg_match requires third argument to be passed by reference
Attempts:
2 left
💡 Hint
Lookbehind in PHP requires fixed length patterns.
📝 Syntax
advanced
2:00remaining
Which regex pattern uses both lookahead and lookbehind correctly?
Which of the following PHP regex patterns correctly matches 'foo' only if it is preceded by 'bar' and followed by 'baz'?
A/(?<!bar)foo(?!baz)/
B/(?<=bar)foo(?<=baz)/
C/(?=bar)foo(?=baz)/
D/(?<=bar)foo(?=baz)/
Attempts:
2 left
💡 Hint
Lookbehind checks before, lookahead checks after the match.
🚀 Application
expert
3:00remaining
Count words not starting with a digit using lookahead
You want to count how many words in the string "$text = '1apple apple 2banana banana 3cherry cherry'" do not start with a digit. Which PHP code snippet correctly counts these words using lookahead?
PHP
$text = '1apple apple 2banana banana 3cherry cherry';
// Your code here
A
$pattern = '/(?&lt;=\d)\b\w+\b/';
preg_match_all($pattern, $text, $matches);
echo count($matches[0]);
B
$pattern = '/\b(?!\d)\w+\b/';
preg_match_all($pattern, $text, $matches);
echo count($matches[0]);
C
$pattern = '/\b\w+\b(?&lt;!\d)/';
preg_match_all($pattern, $text, $matches);
echo count($matches[0]);
D
$pattern = '/(?&lt;!\d)\w+/';
preg_match_all($pattern, $text, $matches);
echo count($matches[0]);
Attempts:
2 left
💡 Hint
Use negative lookahead to exclude words starting with digits and word boundaries to match whole words.