Challenge - 5 Problems
Regex Mastery Badge
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 regex match?
Consider the following Ruby code snippet. What will be printed when it runs?
Ruby
text = "Email: user@example.com" match = text.match(/\b\w+@\w+\.\w+\b/) puts match[0]
Attempts:
2 left
💡 Hint
Look for the pattern that matches an email-like string.
✗ Incorrect
The regex \b\w+@\w+\.\w+\b matches a word boundary, then one or more word characters, an '@', more word characters, a dot, and word characters again. It matches the email part in the string.
❓ Predict Output
intermediate2:00remaining
What does this regex find in the string?
Given the Ruby code below, what will be the output?
Ruby
text = "Call me at 123-456-7890 or 987-654-3210." numbers = text.scan(/\d{3}-\d{3}-\d{4}/) puts numbers.join(", ")
Attempts:
2 left
💡 Hint
Look for the pattern matching phone numbers with dashes.
✗ Incorrect
The regex \d{3}-\d{3}-\d{4} matches three digits, a dash, three digits, a dash, and four digits, which matches the phone numbers in the string.
🔧 Debug
advanced2:30remaining
Why does this regex fail to match?
The following Ruby code is intended to find all words starting with a capital letter. Why does it not find any matches?
Ruby
text = "I." matches = text.scan(/[A-Z][a-z]+/) puts matches
Attempts:
2 left
💡 Hint
Check what the pattern requires after the capital letter.
✗ Incorrect
The regex [A-Z][a-z]+ matches a capital letter followed by one or more lowercase letters. Single capital letters like 'I' have no lowercase letters after them, so they are not matched.
📝 Syntax
advanced2:30remaining
Which regex pattern correctly matches a hexadecimal color code?
You want to match strings like '#1a2b3c' or '#ABCDEF' in Ruby. Which pattern is correct?
Attempts:
2 left
💡 Hint
Hex digits are 0-9 and a-f (case insensitive).
✗ Incorrect
Option A matches a '#' followed by exactly 6 hex digits (0-9, a-f, A-F) and a word boundary. Option A is case-insensitive but misses the word boundary and is less precise. Option A includes invalid characters 'g-z'. Option A allows 3 to 6 digits, which is not strictly a 6-digit hex code.
🚀 Application
expert3:00remaining
How many matches does this regex find?
In Ruby, given the string and regex below, how many matches will be found?
Ruby
text = "abc123xyz456def789" matches = text.scan(/[a-z]+\d+/) puts matches.size
Attempts:
2 left
💡 Hint
Look for sequences of letters followed immediately by digits.
✗ Incorrect
The regex [a-z]+\d+ matches one or more lowercase letters followed by one or more digits. The string has 'abc123', 'xyz456', and 'def789' matching this pattern, so 3 matches.