Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete 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'
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 '(?=)'.
✗ Incorrect
The syntax '(?=bar)' is a positive lookahead that ensures 'foo' is followed by 'bar'.
2fill in blank
mediumComplete 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'
Attempts:
3 left
💡 Hint
Common Mistakes
Using '(?=foo)' which is a lookahead, not lookbehind.
Using '(?
Using '(?!foo)' which is a negative lookahead.
✗ Incorrect
The syntax '(?<=foo)' is a positive lookbehind that ensures 'bar' is preceded by 'foo'.
3fill in blank
hardFix 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'
Attempts:
3 left
💡 Hint
Common Mistakes
Using '(?
Using '(?=dog)' which is a positive lookahead.
Leaving the lookahead empty as '(?=)'.
✗ Incorrect
The syntax '(?!dog)' is a negative lookahead that ensures 'cat' is not followed by 'dog'.
4fill in blank
hardFill 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'
Attempts:
3 left
💡 Hint
Common Mistakes
Using '(?=red)' instead of negative lookahead.
Using '(?
Mixing up lookahead and lookbehind positions.
✗ Incorrect
Use '(?<=green)' for positive lookbehind and '(?!red)' for negative lookahead.
5fill in blank
hardFill 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'
Attempts:
3 left
💡 Hint
Common Mistakes
Using '(?
Not using parentheses around alternatives in lookbehind.
Mixing up the order of lookbehind and lookahead.
✗ Incorrect
Use '(?<=big|fat)' for lookbehind and '(?!cat)' for negative lookahead after 'dog'.