0
0
Rubyprogramming~10 mins

Regex literal syntax (/pattern/) in Ruby - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to create a regex that matches the word 'cat'.

Ruby
pattern = /[1]/
Drag options to blanks, or click blank then click option'
Acat
Bdog
Cbat
Drat
Attempts:
3 left
💡 Hint
Common Mistakes
Using a different word inside the slashes.
Forgetting the slashes around the pattern.
2fill in blank
medium

Complete the code to check if the string 'hello' contains the letter 'e'.

Ruby
if 'hello' =~ /[1]/
  puts 'Found e'
end
Drag options to blanks, or click blank then click option'
Aa
Bo
Ci
De
Attempts:
3 left
💡 Hint
Common Mistakes
Choosing a letter not present in the string.
Using quotes inside the regex slashes.
3fill in blank
hard

Fix the error in the regex literal to match any digit.

Ruby
pattern = /[1]/
Drag options to blanks, or click blank then click option'
A[d]
Bd
C\d
D(d)
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'd' without backslash matches the letter 'd', not digits.
Using square brackets or parentheses incorrectly.
4fill in blank
hard

Fill both blanks to create a regex that matches a word starting with 'a' and ending with 'z'.

Ruby
pattern = /[1].*[2]/
Drag options to blanks, or click blank then click option'
A^a
Ba
Cz$
D$z
Attempts:
3 left
💡 Hint
Common Mistakes
Placing anchors in the wrong order.
Using '$z' instead of 'z$'.
5fill in blank
hard

Fill all three blanks to create a regex that matches a string with exactly three digits.

Ruby
pattern = /^[1][2][3]$/
Drag options to blanks, or click blank then click option'
A\d
B[0-9]
C\w
D\s
Attempts:
3 left
💡 Hint
Common Mistakes
Using \w which matches letters and digits.
Using \s which matches whitespace.