0
0
Rubyprogramming~20 mins

Regex literal syntax (/pattern/) in Ruby - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Regex Literal Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2: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)
ASyntaxError
Bfalse
Cnil
Dtrue
Attempts:
2 left
💡 Hint
The regex literal /World/ matches the substring 'World' in the text.
Predict Output
intermediate
2: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]
A123
B\d{3}
Cabc
Dnil
Attempts:
2 left
💡 Hint
The regex /\d{3}/ looks for exactly three digits in a row.
Predict Output
advanced
2: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)
Afalse
Btrue
Cnil
DSyntaxError
Attempts:
2 left
💡 Hint
The regex uses ^ and $ anchors to match the whole string from start to end.
Predict Output
advanced
2:00remaining
What error does this Ruby regex literal cause?
What error will this Ruby code raise?
Ruby
pattern = /unclosed
puts pattern
ASyntaxError
BNo error, prints /unclosed/
CRuntimeError
DNameError
Attempts:
2 left
💡 Hint
Regex literals must be closed on the same line in Ruby.
🧠 Conceptual
expert
2: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
A1
B3
C4
D0
Attempts:
2 left
💡 Hint
The pattern matches a word followed by a digit.