Bird
0
0

You want to extract the first word starting with a capital letter from the string "today is a Sunny day" using match. Which code correctly does this and prints the matched word?

hard📝 Application Q15 of 15
Ruby - Regular Expressions
You want to extract the first word starting with a capital letter from the string "today is a Sunny day" using match. Which code correctly does this and prints the matched word?
Aputs "today is a Sunny day".match(/[A-Z]+/)[0]
Bputs "today is a Sunny day".match(/[a-z]\w*/)[0]
Cputs "today is a Sunny day".match(/\b\w+/)[0]
Dputs "today is a Sunny day".match(/[A-Z]\w*/)[0]
Step-by-Step Solution
Solution:
  1. Step 1: Understand the regex requirements

    We want the first word starting with a capital letter. Pattern /[A-Z]\w*/ matches a capital letter followed by zero or more word characters.
  2. Step 2: Analyze each option

    puts "today is a Sunny day".match(/[A-Z]\w*/)[0] matches correctly and returns the first capitalized word "Sunny". puts "today is a Sunny day".match(/[a-z]\w*/)[0] matches lowercase start, first is "today", wrong. puts "today is a Sunny day".match(/\b\w+/)[0] matches any word, first is "today", not capitalized. puts "today is a Sunny day".match(/[A-Z]+/)[0] matches only consecutive capital letters, first is "S", not full word.
  3. Final Answer:

    puts "today is a Sunny day".match(/[A-Z]\w*/)[0] -> Option D
  4. Quick Check:

    Capitalized word match = puts "today is a Sunny day".match(/[A-Z]\w*/)[0] [OK]
Quick Trick: Use regex starting with [A-Z] and access [0] from match [OK]
Common Mistakes:
  • Using lowercase letter class instead of uppercase
  • Matching only letters without full word
  • Not accessing matched text with [0]

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Ruby Quizzes