A character class lets you match any one character from a set of characters. It is written inside square brackets, like <code>[abc]</code>, which matches 'a', 'b', or 'c'.
Click to reveal answer
beginner
What does the quantifier + mean in a regex?
The + quantifier means "one or more" of the preceding element. For example, a+ matches 'a', 'aa', 'aaa', and so on.
Click to reveal answer
intermediate
Explain the difference between * and ? quantifiers.
* means "zero or more" of the preceding element, so it can match nothing or many. ? means "zero or one", so it matches either nothing or exactly one occurrence.
Click to reveal answer
beginner
What does the character class \d match?
\d matches any digit character, which means any number from 0 to 9.
Click to reveal answer
beginner
How do you specify a range of characters in a character class?
You use a hyphen - inside square brackets. For example, [a-z] matches any lowercase letter from 'a' to 'z'.
Click to reveal answer
What does the regex [A-Z]+ match?
AOne or more uppercase letters
BOne or more lowercase letters
CAny single uppercase letter
DAny single digit
✗ Incorrect
The character class [A-Z] matches any uppercase letter, and + means one or more times.
Which quantifier matches zero or one occurrence of the previous character?
A*
B+
C?
D{2,}
✗ Incorrect
The ? quantifier matches zero or one occurrence.
What does \w match in PHP regex?
AAny whitespace character
BAny word character (letters, digits, underscore)
CAny digit
DAny special character
✗ Incorrect
\w matches letters, digits, and underscore characters.
How would you match exactly 3 digits in a row?
A\d{3}
B\d+
C\d*
D\d?
✗ Incorrect
The quantifier {3} means exactly 3 occurrences.
What does the regex [a-zA-Z0-9] match?
AAny letter or digit
BOnly lowercase letters
COnly uppercase letters
DOnly digits
✗ Incorrect
The character class includes lowercase letters, uppercase letters, and digits.
Describe how character classes and quantifiers work together in regular expressions.
Think about how you pick letters and how many times they repeat.
You got /3 concepts.
Explain the difference between the quantifiers *, +, and ? with examples.
Focus on how many times the character can appear.
You got /4 concepts.
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
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.
Step 2: Identify the digit matcher
The class that matches digits is \\d.
Final Answer:
\\d -> Option D
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?