0
0
Rubyprogramming~5 mins

Common patterns and character classes in Ruby - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
Recall & Review
beginner
What does the character class [a-z] represent in Ruby regular expressions?
It matches any single lowercase letter from a to z.
Click to reveal answer
beginner
What does the pattern \d match in Ruby regular expressions?
It matches any single digit character, equivalent to [0-9].
Click to reveal answer
intermediate
Explain the difference between \w and \W in Ruby regex.
\w matches any word character (letters, digits, and underscore), while \W matches any character that is NOT a word character.
Click to reveal answer
beginner
What does the pattern \s match in Ruby regular expressions?
It matches any whitespace character, including spaces, tabs, and newline characters.
Click to reveal answer
beginner
How do you match any character except a newline in Ruby regex?
Use the dot . character, which matches any character except newline by default.
Click to reveal answer
Which pattern matches any uppercase letter in Ruby regex?
A\d
B[A-Z]
C[a-z]
D\s
What does the pattern \D match?
AAny non-digit
BAny digit
CAny whitespace
DAny word character
Which pattern matches a whitespace character?
A\d
B\w
C\s
D\W
What does the pattern [^a-z] mean?
AMatches any lowercase letter
BMatches whitespace only
CMatches digits only
DMatches any character except lowercase letters
Which pattern matches any word character?
A\w
B\D
C\s
D\W
Describe the common character classes used in Ruby regular expressions and give an example of each.
Think about digits, letters, and spaces.
You got /5 concepts.
    Explain how to use negation inside character classes in Ruby regex and provide an example pattern.
    Negation flips what the class matches.
    You got /3 concepts.