Bird
Raised Fist0
PHPprogramming~20 mins

Character classes and quantifiers in PHP - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Regex Mastery in PHP
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2:00remaining
Output of regex match with character classes
What will be the output of this PHP code snippet?
PHP
<?php
$pattern = '/[a-c]{2,4}/';
$subject = 'abcabcabc';
preg_match_all($pattern, $subject, $matches);
print_r($matches[0]);
?>
AArray ( [0] => abca [1] => bcab )
BArray ( [0] => ab [1] => ca [2] => bc )
CArray ( [0] => a [1] => b [2] => c )
DArray ( [0] => abc [1] => abc [2] => abc )
Attempts:
2 left
💡 Hint
Look at the pattern: it matches 2 to 4 characters from a to c.
❓ Predict Output
intermediate
2:00remaining
Result of regex with negated character class
What will this PHP code output?
PHP
<?php
$pattern = '/[^0-9]{3}/';
$subject = 'abc123d4';
preg_match_all($pattern, $subject, $matches);
print_r($matches[0]);
?>
AArray ( [0] => a1b [1] => 2c3 [2] => d4 )
BArray ( [0] => b2c )
CArray ( [0] => bcd )
DArray ( [0] => abc )
Attempts:
2 left
💡 Hint
The pattern matches 3 characters that are NOT digits.
🔧 Debug
advanced
2:00remaining
Identify the error in regex quantifier usage
This PHP code throws an error. What is the cause?
PHP
<?php
$pattern = '/[a-z]{3,2}/';
$subject = 'abcdef';
preg_match($pattern, $subject, $matches);
print_r($matches);
?>
ARuntimeError: preg_match failed due to invalid pattern
BNo error, matches 'abc'
CSyntaxError: Quantifier range is invalid because 3 is greater than 2
DTypeError: preg_match expects string, got array
Attempts:
2 left
💡 Hint
Check the quantifier {3,2} carefully.
📝 Syntax
advanced
2:00remaining
Which regex pattern matches exactly 1 or 2 digits?
Choose the correct regex pattern that matches exactly one or two digits in PHP.
A'/\d{1,2}/'
B'/\d{1,}/'
C'/\d{,2}/'
D'/\d{2,1}/'
Attempts:
2 left
💡 Hint
Quantifiers specify minimum and maximum counts in the form {min,max}.
🚀 Application
expert
3:00remaining
Count words with 3 to 5 letters using regex
You want to count how many words in a string have between 3 and 5 letters (only letters a-z, case insensitive). Which PHP code snippet correctly does this?
A
&lt;?php
preg_match_all('/\b[a-zA-Z]{3,5}/', $text, $matches);
echo count($matches[0]);
?&gt;
B
&lt;?php
preg_match_all('/\b[a-z]{3,5}\b/i', $text, $matches);
echo count($matches[0]);
?&gt;
C
&lt;?php
preg_match_all('/\b[a-zA-Z]{3,5}\b/', $text, $matches);
echo count($matches[0]);
?&gt;
D
&lt;?php
preg_match_all('/[a-zA-Z]{3,5}\b/', $text, $matches);
echo count($matches[0]);
?&gt;
Attempts:
2 left
💡 Hint
Use word boundaries and case-insensitive flag to match words correctly.

Practice

(1/5)
1. Which character class in PHP regex matches any digit from 0 to 9?
/[?]/
easy
A. \\D
B. \\w
C. \\s
D. \\d

Solution

  1. Step 1: Understand character classes

    In PHP regex, \\d matches any digit (0-9), \\w matches word characters, \\s matches whitespace, and \\D matches non-digits.
  2. Step 2: Identify the digit matcher

    The class that matches digits is \\d.
  3. Final Answer:

    \\d -> Option D
  4. Quick Check:

    Digits = \\d [OK]
Hint: Remember: \d = digit, \w = word, \s = space [OK]
Common Mistakes:
  • Confusing \d with \w
  • Using \D to match digits
  • Mixing up \s with digit class
2. Which of the following is the correct syntax to match exactly three lowercase letters using PHP regex?
easy
A. /[a-z]{3}/
B. /[a-z]{,3}/
C. /[a-z]{3,}/
D. /[a-z]{3,5}/

Solution

  1. Step 1: Understand quantifiers

    Curly braces {n} specify exact count. {3} means exactly 3 times.
  2. Step 2: Match exactly three lowercase letters

    The pattern /[a-z]{3}/ matches exactly three lowercase letters.
  3. Final Answer:

    /[a-z]{3}/ -> Option A
  4. Quick Check:

    Exact 3 lowercase letters = /[a-z]{3}/ [OK]
Hint: Use {n} for exact count in regex quantifiers [OK]
Common Mistakes:
  • Using {,3} which is invalid syntax
  • Using {3,} which means 3 or more
  • Using {3,5} which means 3 to 5 times
3. What will be the output of this PHP code?
$pattern = '/\d{2,4}/';
$string = 'Year 2023 and 99 problems';
preg_match_all($pattern, $string, $matches);
print_r($matches[0]);
medium
A. ["20", "23", "99"]
B. ["2023", "99"]
C. ["202", "3", "99"]
D. ["2", "0", "2", "3", "9", "9"]

Solution

  1. Step 1: Understand the regex pattern

    The pattern /\d{2,4}/ matches sequences of digits between 2 and 4 digits long.
  2. Step 2: Apply pattern to string

    In 'Year 2023 and 99 problems', '2023' (4 digits) and '99' (2 digits) match. Smaller digit groups like '2' or '3' alone do not match.
  3. Final Answer:

    ["2023", "99"] -> Option B
  4. Quick Check:

    Digits 2-4 long = ["2023", "99"] [OK]
Hint: Quantifier {2,4} matches digit groups length 2 to 4 [OK]
Common Mistakes:
  • Splitting digits into smaller groups
  • Ignoring quantifier range
  • Assuming single digits match
4. Identify the error in this PHP regex pattern:
$pattern = '/[a-z]{2,1}/';
medium
A. Quantifier range is invalid; min cannot be greater than max
B. Character class brackets are missing
C. Pattern should use double quotes instead of single quotes
D. No error; pattern is valid

Solution

  1. Step 1: Analyze quantifier syntax

    The quantifier {2,1} means minimum 2 and maximum 1 repetitions, which is invalid because min > max.
  2. Step 2: Confirm error type

    Regex quantifiers must have min ≤ max. This causes a syntax error.
  3. Final Answer:

    Quantifier range is invalid; min cannot be greater than max -> Option A
  4. Quick Check:

    Min > max in {2,1} is invalid [OK]
Hint: Quantifier {min,max} must have min ≤ max [OK]
Common Mistakes:
  • Swapping min and max values
  • Ignoring quantifier syntax rules
  • Assuming pattern is valid without checking quantifiers
5. You want to match a string that contains exactly 3 to 5 uppercase letters followed by 2 digits in PHP regex. Which pattern is correct?
hard
A. /^[A-Z]{3,5}\d{1,2}$/
B. /^[A-Z]{2,3}\d{5}$/
C. /^[A-Z]{3,5}\d{2}$/
D. /^[A-Z]{3,5}\d{3}$/

Solution

  1. Step 1: Understand the requirements

    The string must start (^) with 3 to 5 uppercase letters [A-Z]{3,5} and end ($) with exactly 2 digits \d{2}.
  2. Step 2: Match pattern to options

    /^[A-Z]{3,5}\d{2}$/ matches exactly 3-5 uppercase letters followed by exactly 2 digits, anchored start and end.
  3. Final Answer:

    /^[A-Z]{3,5}\d{2}$/ -> Option C
  4. Quick Check:

    3-5 uppercase + 2 digits = /^[A-Z]{3,5}\d{2}$/ [OK]
Hint: Use anchors ^ and $ to match whole string exactly [OK]
Common Mistakes:
  • Mixing digit counts in quantifiers
  • Omitting anchors causing partial matches
  • Using wrong quantifier ranges
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard