Challenge - 5 Problems
Regex Literal Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
What is the output of this Ruby code using regex literal?
Consider the following Ruby code snippet. What will it print?
Ruby
text = "Hello World" pattern = /World/ puts text.match?(pattern)
Attempts:
2 left
💡 Hint
The regex literal /World/ matches the substring 'World' in the text.
✗ Incorrect
The method match? returns true if the regex matches anywhere in the string. Since 'World' is present, it returns true.
❓ Predict Output
intermediate2:00remaining
What does this regex literal match in Ruby?
What will the following Ruby code output?
Ruby
text = "abc123xyz" pattern = /\d{3}/ match = text.match(pattern) puts match[0]
Attempts:
2 left
💡 Hint
The regex /\d{3}/ looks for exactly three digits in a row.
✗ Incorrect
The pattern matches '123' in the string, so match[0] returns '123'.
❓ Predict Output
advanced2:00remaining
What is the output of this Ruby regex literal with anchors?
What will this Ruby code print?
Ruby
text = "start middle end" pattern = /^start.*end$/ puts text.match?(pattern)
Attempts:
2 left
💡 Hint
The regex uses ^ and $ anchors to match the whole string from start to end.
✗ Incorrect
The pattern matches the entire string because it starts with 'start' and ends with 'end' with any characters in between.
❓ Predict Output
advanced2:00remaining
What error does this Ruby regex literal cause?
What error will this Ruby code raise?
Ruby
pattern = /unclosed
puts patternAttempts:
2 left
💡 Hint
Regex literals must be closed on the same line in Ruby.
✗ Incorrect
The regex literal is not closed properly, causing a SyntaxError.
🧠 Conceptual
expert2:00remaining
How many matches does this Ruby regex literal find?
Given the code below, how many matches will be found?
Ruby
text = "one1 two2 three3 four4" pattern = /\w+\d/ matches = text.scan(pattern) puts matches.size
Attempts:
2 left
💡 Hint
The pattern matches a word followed by a digit.
✗ Incorrect
The scan method finds all substrings matching the pattern. Here, 'one1', 'two2', 'three3', and 'four4' all match.