Bird
0
0

Which of the following is a correct way to write a regex literal that matches any digit in Ruby?

easy📝 Conceptual Q2 of 15
Ruby - Regular Expressions
Which of the following is a correct way to write a regex literal that matches any digit in Ruby?
A/\d/
B/d/
C/[digit]/
D/\D/
Step-by-Step Solution
Solution:
  1. Step 1: Recall regex digit shorthand

    In Ruby regex, \d matches any digit character (0-9).
  2. Step 2: Check each option

    /\d/ uses /\d/ which is correct. /d/ matches literal 'd'. /[digit]/ matches any character 'd', 'i', 'g', 'i', 't'. /\D/ matches non-digit characters.
  3. Final Answer:

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

    Digit regex = /\d/ [OK]
Quick Trick: Use /\d/ to match digits in Ruby regex [OK]
Common Mistakes:
  • Forgetting to escape backslash in regex literal
  • Using /d/ which matches letter 'd', not digits
  • Confusing \d with \D (non-digit)

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Ruby Quizzes