0
0
PHPprogramming~10 mins

Capturing groups and backreferences in PHP - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Capturing groups and backreferences
Start regex
Find ( ) capturing group
Match pattern inside group
Store matched text
Use backreference \1 to match same text again
Full pattern matched?
NoFail
Yes
Return match with captured groups
The regex engine finds text matching the pattern inside parentheses, stores it, then uses \1 to match the same text again.
Execution Sample
PHP
<?php
$pattern = '/(\w+) \1/';
$subject = 'hello hello world';
preg_match($pattern, $subject, $matches);
print_r($matches);
?>
This code finds repeated words like 'hello hello' using a capturing group and backreference.
Execution Table
StepRegex PositionActionMatched TextCaptured Group 1Backreference \1 MatchResult
1StartBegin matching at start of subjectContinue
2(\w+)Match first word charactershellohelloContinue
3 Match space character helloContinue
4\1Match same text as group 1hellohellohelloContinue
5EndPattern fully matchedhello hellohellohelloMatch found
6After matchStop searchinghellohelloExit
💡 Pattern matched 'hello hello' and stopped searching further.
Variable Tracker
VariableStartAfter Step 2After Step 4Final
Captured Group 1hellohellohello
Backreference \1hellohello
Key Moments - 2 Insights
Why does the backreference \1 match the same text as the first group?
Because \1 refers exactly to the text captured by the first parentheses group, as shown in execution_table step 4.
What happens if the backreference \1 does not find the same text?
The pattern fails to match and the regex engine stops, as no full match is found (not shown in this successful trace).
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what text is captured by group 1 at step 2?
Ahello
Bworld
Chello hello
D\1
💡 Hint
Check the 'Captured Group 1' column at step 2 in the execution_table.
At which step does the backreference \1 successfully match the same text?
AStep 2
BStep 4
CStep 5
DStep 6
💡 Hint
Look at the 'Backreference \1 Match' column in the execution_table.
If the subject was 'hello world', what would happen to the match?
AMatch found with 'hello world'
BNo match because backreference \1 fails
CMatch found with 'world world'
DMatch found with 'hello hello'
💡 Hint
Backreference \1 must match the exact text captured by group 1; see key_moments about failure.
Concept Snapshot
Capturing groups use parentheses ( ) to save matched text.
Backreferences like \1 match the exact text from the first group.
Used to find repeated patterns, e.g., repeated words.
If backreference fails, the whole match fails.
Syntax example: /(\w+) \1/ matches repeated words.
Full Transcript
This example shows how PHP regex uses capturing groups and backreferences. The pattern /(\w+) \1/ looks for a word, then the same word again separated by space. Step by step, the regex matches 'hello' as group 1, then matches a space, then uses \1 to match the same 'hello' again. The match succeeds with 'hello hello'. Variables track the captured group and backreference text. If the backreference does not match, the pattern fails. This helps find repeated text patterns easily.