0
0
PHPprogramming~10 mins

Character classes and quantifiers in PHP - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Character classes and quantifiers
Start
Define regex pattern
Input string
Match regex
Check character classes
Apply quantifiers
Return matches
End
The program defines a regex pattern with character classes and quantifiers, then checks an input string for matches step-by-step.
Execution Sample
PHP
<?php
$pattern = '/[a-z]{2,4}\d+/';
$input = 'ab12 xyz123 abcdef456';
preg_match_all($pattern, $input, $matches);
print_r($matches[0]);
?>
This PHP code finds all substrings with 2 to 4 lowercase letters followed by one or more digits.
Execution Table
StepActionPattern PartInput SegmentMatch ResultOutput
1Define pattern[a-z]{2,4}\d+-Pattern ready-
2Input string-ab12 xyz123 abcdef456Input ready-
3Match attempt 1[a-z]{2,4}\d+ab12Matches 'ab12'['ab12']
4Match attempt 2[a-z]{2,4}\d+xyz123Matches 'xyz123'['ab12', 'xyz123']
5Match attempt 3[a-z]{2,4}\d+abcdef456No match (letters >4)['ab12', 'xyz123']
6End--Matching complete['ab12', 'xyz123']
💡 No more matches found; input fully scanned.
Variable Tracker
VariableStartAfter Step 3After Step 4After Step 5Final
$pattern/[a-z]{2,4}\d+//[a-z]{2,4}\d+//[a-z]{2,4}\d+//[a-z]{2,4}\d+//[a-z]{2,4}\d+/
$inputab12 xyz123 abcdef456ab12 xyz123 abcdef456ab12 xyz123 abcdef456ab12 xyz123 abcdef456ab12 xyz123 abcdef456
$matches[0][]['ab12']['ab12', 'xyz123']['ab12', 'xyz123']['ab12', 'xyz123']
Key Moments - 2 Insights
Why does 'abcdef456' not match the pattern even though it has letters followed by digits?
Because the pattern requires 2 to 4 lowercase letters ([a-z]{2,4}) before digits, but 'abcdef' has 6 letters, exceeding the quantifier limit. See execution_table row 5.
What does the quantifier '+' after \d mean in the pattern?
It means one or more digits must follow the letters. So at least one digit is required for a match. This is why 'ab12' and 'xyz123' match. See execution_table rows 3 and 4.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the value of $matches[0] after step 4?
A['ab12']
B['xyz123']
C['ab12', 'xyz123']
D[]
💡 Hint
Check the Output column at step 4 in execution_table.
At which step does the pattern fail to match due to too many letters?
AStep 3
BStep 5
CStep 4
DStep 6
💡 Hint
Look at the Match Result column where it says 'No match (letters >4)'.
If the quantifier {2,4} was changed to {3,5}, which input segment would fail to match?
A'ab12'
B'xyz123'
C'abcdef456'
DAll would match
💡 Hint
Check variable_tracker for $matches and consider letter counts in input segments.
Concept Snapshot
Character classes: [a-z] matches any lowercase letter.
Quantifiers: {2,4} means match 2 to 4 times.
+ means one or more times.
Together: [a-z]{2,4}\d+ matches 2-4 letters followed by digits.
Used in PHP with preg_match_all to find matches in strings.
Full Transcript
This example shows how PHP uses regex with character classes and quantifiers. The pattern '/[a-z]{2,4}\d+/' looks for substrings with 2 to 4 lowercase letters followed by one or more digits. The input string 'ab12 xyz123 abcdef456' is scanned. Matches found are 'ab12' and 'xyz123'. 'abcdef456' does not match because it has 6 letters, exceeding the {2,4} limit. The execution table traces each matching attempt step-by-step, showing how variables change. Key points include understanding quantifiers and why some substrings fail to match. The visual quiz tests understanding of these steps and pattern behavior.