Bird
0
0

Which regex correctly captures these parts?

hard📝 Application Q8 of 15
Ruby - Regular Expressions
Given a string with multiple dates like '2023-05-01 and 2024-06-15', write a Ruby regex with capture groups to extract year, month, and day separately for each date. Which regex correctly captures these parts?
A/(\d{4})-(\d{2})-(\d{2})/
B/(\d{2})-(\d{2})-(\d{4})/
C/(\d{4})\/(\d{2})\/(\d{2})/
D/(\d{4})(\d{2})(\d{2})/
Step-by-Step Solution
Solution:
  1. Step 1: Analyze date format in string

    Dates are in 'YYYY-MM-DD' format with dashes.
  2. Step 2: Match regex to format

    /(\d{4})-(\d{2})-(\d{2})/ matches four digits, dash, two digits, dash, two digits with capture groups.
  3. Final Answer:

    /(\d{4})-(\d{2})-(\d{2})/ -> Option A
  4. Quick Check:

    Year-month-day with dashes = /(\d{4})-(\d{2})-(\d{2})/ [OK]
Quick Trick: Match exact separators and digit counts in capture groups [OK]
Common Mistakes:
  • Swapping month and year positions
  • Using wrong separators like slashes
  • Missing dashes between groups

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Ruby Quizzes