Bird
Raised Fist0
Rubyprogramming~20 mins

Common patterns and character classes in Ruby - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Regex Mastery Badge
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 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]
AEmail
Bnil
Cexample.com
Duser@example.com
Attempts:
2 left
💡 Hint
Look for the pattern that matches an email-like string.
❓ Predict Output
intermediate
2: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(", ")
ACall, me, at
B1234567890, 9876543210
C123-456-7890, 987-654-3210
D[]
Attempts:
2 left
💡 Hint
Look for the pattern matching phone numbers with dashes.
🔧 Debug
advanced
2: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
AIt misses single-letter capital words like 'I' because the pattern requires at least one lowercase letter after the capital letter.
BThe regex is case-insensitive by default, so it matches everything.
CThe pattern uses wrong character classes; it should use \w instead of [a-z].
DThe scan method returns nil if no matches are found.
Attempts:
2 left
💡 Hint
Check what the pattern requires after the capital letter.
📝 Syntax
advanced
2: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?
A/#([0-9a-fA-F]{6})\b/
B/#([0-9g-zA-Z]{6})/
C/#([0-9a-f]{6})/i
D/#([0-9a-fA-F]{3,6})/
Attempts:
2 left
💡 Hint
Hex digits are 0-9 and a-f (case insensitive).
🚀 Application
expert
3: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
A1
B3
C0
D2
Attempts:
2 left
💡 Hint
Look for sequences of letters followed immediately by digits.

Practice

(1/5)
1. Which Ruby regular expression pattern matches any single digit character?
easy
A. /\D/
B. /\d/
C. /\s/
D. /\w/

Solution

  1. Step 1: Understand \d in Ruby regex

    The pattern \d matches any digit character (0-9).
  2. Step 2: Compare with other options

    \w matches word characters (letters, digits, underscore), \s matches whitespace, \D matches non-digit characters.
  3. Final Answer:

    /\d/ -> Option B
  4. Quick Check:

    Digit pattern = /\d/ [OK]
Hint: Remember \d means digit, \w means word character [OK]
Common Mistakes:
  • Confusing \d with \w
  • Using /\D/ which matches non-digits
  • Thinking \s matches digits
2. Which of the following is the correct Ruby syntax to match any word character one or more times?
easy
A. /\w+/
B. /\w*/
C. /\w?/
D. /\w{}/

Solution

  1. Step 1: Understand quantifiers in regex

    + means one or more times, * means zero or more, ? means zero or one.
  2. Step 2: Apply to \w

    /\w+/ matches one or more word characters, which is the correct syntax.
  3. Final Answer:

    /\w+/ -> Option A
  4. Quick Check:

    One or more word chars = /\w+/ [OK]
Hint: Use + for one or more matches in regex [OK]
Common Mistakes:
  • Using * which allows zero matches
  • Using ? which allows zero or one match
  • Using empty braces {} which is invalid
3. What is the output of this Ruby code?
text = "Room 101"
match = text.match(/\d+/)
puts match[0]
medium
A. 101
B. Room
C. 1
D. nil

Solution

  1. Step 1: Understand the regex /\d+/

    This pattern matches one or more digits in a row.
  2. Step 2: Apply match to string "Room 101"

    The digits "101" are matched as a group, so match[0] returns "101".
  3. Final Answer:

    101 -> Option A
  4. Quick Check:

    Digits matched = 101 [OK]
Hint: \d+ grabs all digits in a row, not just one [OK]
Common Mistakes:
  • Thinking it matches only one digit
  • Confusing match[0] with match object
  • Expecting the word part instead of digits
4. Find the error in this Ruby code snippet:
email = "user@example.com"
if email =~ /\w+@\w+.\w+/
  puts "Valid email"
else
  puts "Invalid email"
end
medium
A. The regex should use \d instead of \w
B. The =~ operator is used incorrectly
C. The regex does not escape the dot properly
D. The string should be in single quotes

Solution

  1. Step 1: Examine the regex pattern

    The pattern /\w+@\w+.\w+/ uses an unescaped . which matches any character, not a literal dot.
  2. Step 2: Identify the error

    The dot after the domain must be escaped as \. to match the literal "." in email addresses.
  3. Final Answer:

    The regex does not escape the dot properly -> Option C
  4. Quick Check:

    Escape literal dot as \. [OK]
Hint: Remember to escape dot as \. to match literal dot [OK]
Common Mistakes:
  • Thinking dot doesn't need escaping
  • Misusing =~ operator
  • Confusing \w with \d in email regex
5. You want to extract all words starting with a capital letter from the string "Hello World from Ruby". Which Ruby regex pattern should you use with scan?
hard
A. /\d+/
B. /\b[a-z]\w*/
C. /\s+/
D. /\b[A-Z]\w*/

Solution

  1. Step 1: Understand the requirement

    We want words starting with a capital letter, so first letter must be A-Z.
  2. Step 2: Analyze the regex options

    /\b[A-Z]\w*/ matches word boundary, then capital letter, then zero or more word chars.
  3. Step 3: Check other options

    /\b[a-z]\w*/ matches lowercase start, /\d+/ matches digits, /\s+/ matches spaces, so they don't fit.
  4. Final Answer:

    /\b[A-Z]\w*/ -> Option D
  5. Quick Check:

    Capital-start words = /\b[A-Z]\w*/ [OK]
Hint: Use \b for word boundary and [A-Z] for capital letters [OK]
Common Mistakes:
  • Using [a-z] instead of [A-Z]
  • Forgetting word boundary \b
  • Using digit or space patterns incorrectly
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard