Complete the code to capture the word 'cat' from the string.
text = "The cat sat on the mat." match = text.match(/[1]/) puts match[0]
The regular expression /cat/ matches the substring 'cat' in the text.
Complete the code to capture the first word using a capture group.
text = "Hello world" match = text.match(/(\w+)[1]/) puts match[1]
The pattern /(\w+)\s/ captures one or more word characters followed by a whitespace.
Fix the error in the code to correctly capture the domain from the email.
email = "user@example.com" domain = email.match(/@(\w+[1]\w+)/)[1] puts domain
The pattern /@(\w+\.\w+)/ captures the domain part after '@', including the dot.
Fill both blanks to capture the area code and the phone number separately.
phone = "(123) 456-7890" match = phone.match(/\(([1])\) ([2])-\d{4}/) puts match[1] puts match[2]
The pattern captures three digits inside parentheses as the area code and three digits after the space as the phone number part.
Fill all three blanks to create a hash with keys as captured words and values as their lengths.
text = "red blue green" result = text.scan(/(\w+) (\w+) (\w+)/).map do |[1], [2], [3]| { [1] => [1].length, [2] => [2].length, [3] => [3].length } end puts result
The block variables a, b, c capture the three words, which are then used as keys with their lengths as values.