0
0
PHPprogramming~10 mins

Lookahead and lookbehind in PHP - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Lookahead and lookbehind
Start regex engine
Check lookbehind condition
Yes
Match main pattern
Check lookahead condition
Yes
Accept or reject match
The regex engine first checks the lookbehind condition before the main pattern, then matches the main pattern, and finally checks the lookahead condition after the main pattern to decide if the match is accepted.
Execution Sample
PHP
<?php
$pattern = '/(?<=\$)\d+(?=\sUSD)/';
$text = 'The price is $100 USD.';
preg_match($pattern, $text, $matches);
echo $matches[0];
?>
This code finds a number preceded by '$' and followed by ' USD' in the text.
Execution Table
StepActionText PositionCondition CheckedResultMatch Found
1Check lookbehind (?<=\$)Before '1' in '100'Is previous char '$'?YesContinue
2Match main pattern \d+At '100'Match digitsMatches '100'Continue
3Check lookahead (?=\sUSD)After '100'Is next chars ' USD'?YesAccept match '100'
4End---Match accepted: '100'
💡 Lookbehind and lookahead conditions met, so '100' is matched.
Variable Tracker
VariableStartAfter Step 1After Step 2After Step 3Final
matches[][]['100']['100']['100']
Key Moments - 2 Insights
Why does the regex check the lookbehind before matching digits?
Because lookbehind ensures the pattern before the main match is correct without consuming characters, as shown in step 1 of the execution_table.
What happens if the lookahead condition fails?
The match is rejected even if the main pattern matches, as the lookahead must confirm the following characters, shown in step 3.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is checked at Step 1?
AIf the next characters are digits
BIf the previous character is '$'
CIf the main pattern matches digits
DIf the following characters are ' USD'
💡 Hint
Check the 'Condition Checked' column at Step 1 in execution_table.
At which step does the regex confirm the main digits '100'?
AStep 2
BStep 3
CStep 1
DStep 4
💡 Hint
Look at the 'Match Found' column to see when digits are matched.
If the text was 'The price is 100 USD.' (no '$'), what would happen at Step 1?
ALookbehind check passes
BMain pattern matches anyway
CLookbehind check fails, no match
DLookahead check fails
💡 Hint
Look at Step 1 condition about previous character being '$' in execution_table.
Concept Snapshot
Lookbehind (?<=...) checks text before the match without consuming it.
Lookahead (?=...) checks text after the match without consuming it.
Both are zero-width assertions.
Used to match patterns only if preceded or followed by specific text.
In PHP, use preg_match with these in regex patterns.
Full Transcript
This example shows how PHP regex uses lookbehind and lookahead. The regex pattern '/(?<=\$)\d+(?=\sUSD)/' looks for digits preceded by a dollar sign and followed by a space and 'USD'. The engine first checks the lookbehind condition before matching digits, then matches the digits, and finally checks the lookahead condition. If all conditions pass, the match is accepted. Variables track the matched results. Common confusions include why lookbehind is checked first and what happens if lookahead fails. The visual quiz tests understanding of these steps.