0
0
PHPprogramming~5 mins

Character classes and quantifiers in PHP - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
Recall & Review
beginner
What is a character class in regular expressions?
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
Which quantifier matches zero or one occurrence of the previous character?
A*
B+
C?
D{2,}
What does \w match in PHP regex?
AAny whitespace character
BAny word character (letters, digits, underscore)
CAny digit
DAny special character
How would you match exactly 3 digits in a row?
A\d{3}
B\d+
C\d*
D\d?
What does the regex [a-zA-Z0-9] match?
AAny letter or digit
BOnly lowercase letters
COnly uppercase letters
DOnly 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.