0
0
PHPprogramming~10 mins

Lookahead and lookbehind in PHP - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to match 'foo' only if it is followed by 'bar'.

PHP
$pattern = '/foo[1]bar/';
Drag options to blanks, or click blank then click option'
A(?=bar)
B(?=)
C(?<=bar)
D(?!bar)
Attempts:
3 left
💡 Hint
Common Mistakes
Using '(?<=bar)' which is a lookbehind, not lookahead.
Using '(?!bar)' which is a negative lookahead.
Leaving the lookahead empty as '(?=)'.
2fill in blank
medium

Complete the code to match 'bar' only if it is preceded by 'foo'.

PHP
$pattern = '/[1]bar/';
Drag options to blanks, or click blank then click option'
A(?=foo)
B(?!foo)
C(?<!foo)
D(?<=foo)
Attempts:
3 left
💡 Hint
Common Mistakes
Using '(?=foo)' which is a lookahead, not lookbehind.
Using '(?
Using '(?!foo)' which is a negative lookahead.
3fill in blank
hard

Fix the error in the pattern to match 'cat' only if it is not followed by 'dog'.

PHP
$pattern = '/cat[1]dog/';
Drag options to blanks, or click blank then click option'
A(?!dog)
B(?<!dog)
C(?<=dog)
D(?=)
Attempts:
3 left
💡 Hint
Common Mistakes
Using '(?
Using '(?=dog)' which is a positive lookahead.
Leaving the lookahead empty as '(?=)'.
4fill in blank
hard

Fill both blanks to match 'apple' only if it is preceded by 'green' and not followed by 'red'.

PHP
$pattern = '/[1]apple[2]/';
Drag options to blanks, or click blank then click option'
A(?<=green)
B(?=red)
C(?!red)
D(?<!green)
Attempts:
3 left
💡 Hint
Common Mistakes
Using '(?=red)' instead of negative lookahead.
Using '(?
Mixing up lookahead and lookbehind positions.
5fill in blank
hard

Fill all three blanks to create a pattern that matches 'dog' only if it is preceded by 'big' or 'fat' and not followed by 'cat'.

PHP
$pattern = '/(?<=([1]|[2]))dog[3]cat/';
Drag options to blanks, or click blank then click option'
Abig
Bfat
C(?!
D(?<!
Attempts:
3 left
💡 Hint
Common Mistakes
Using '(?
Not using parentheses around alternatives in lookbehind.
Mixing up the order of lookbehind and lookahead.