Challenge - 5 Problems
Regex Backreference Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
What is the output of this PHP code using capturing groups?
Consider the following PHP code that uses a regular expression with capturing groups. What will it output?
PHP
<?php $subject = "apple banana apple"; $pattern = '/(apple) (banana) \1/'; if (preg_match($pattern, $subject, $matches)) { echo $matches[0]; } else { echo "No match"; } ?>
Attempts:
2 left
💡 Hint
Remember that \1 refers to the first captured group in the pattern.
✗ Incorrect
The pattern matches 'apple banana apple' because \1 matches the first captured group 'apple'. So the full match is 'apple banana apple'.
❓ Predict Output
intermediate2:00remaining
What does this PHP code output with nested capturing groups?
Look at this PHP code using nested capturing groups and backreferences. What will it print?
PHP
<?php $subject = "abcabc"; $pattern = '/(abc)(\1)/'; if (preg_match($pattern, $subject, $matches)) { echo count($matches); } else { echo "No match"; } ?>
Attempts:
2 left
💡 Hint
Count how many capturing groups are matched and stored in $matches.
✗ Incorrect
The pattern has two capturing groups: (abc) and (\1). The second group matches the first group's content again. So $matches has 3 elements: full match, group 1, group 2.
🔧 Debug
advanced2:00remaining
Why does this PHP regex fail to match?
This PHP code tries to match repeated words using backreferences but fails. What is the cause?
PHP
<?php $subject = "test test"; $pattern = "/(\w+) \1/"; if (preg_match($pattern, $subject)) { echo "Matched!"; } else { echo "No match"; } ?>
Attempts:
2 left
💡 Hint
Check how PHP interprets backslashes inside single-quoted vs double-quoted strings.
✗ Incorrect
In PHP double-quoted strings, backslashes need to be escaped. The pattern uses '\1' but inside double quotes it becomes '\1' which is interpreted as an octal escape. It should be '\\1' to represent a backreference.
📝 Syntax
advanced2:00remaining
Which option causes a syntax error in PHP regex with capturing groups?
Identify which PHP regex pattern will cause a syntax error when used in preg_match.
Attempts:
2 left
💡 Hint
Look for unbalanced parentheses in the pattern.
✗ Incorrect
Option D has unbalanced parentheses: '(foo(bar)' is missing a closing parenthesis, causing a regex syntax error.
🚀 Application
expert2:00remaining
How many matches does this PHP code find using backreferences?
This PHP code uses preg_match_all with a pattern that uses backreferences. How many matches will it find?
PHP
<?php $subject = "cat cat dog dog cat"; $pattern = '/(\w+) \1/'; preg_match_all($pattern, $subject, $matches); echo count($matches[0]); ?>
Attempts:
2 left
💡 Hint
Look for repeated words separated by a space in the subject string.
✗ Incorrect
The pattern matches repeated words separated by a space. 'cat cat' and 'dog dog' match, but 'cat' at the end is alone. So there are 2 matches.