📖 Scenario: Imagine you are building a simple program to check if certain words appear in a sentence. You will use Ruby's regex literal syntax to create patterns that help find these words.
🎯 Goal: Learn how to write regex patterns using Ruby's /pattern/ syntax and use them to check if words exist in a sentence.
📋 What You'll Learn
Create a string variable with a sentence
Create a regex pattern using literal syntax to find a specific word
Use the regex pattern to check if the word exists in the sentence
Print the result of the check
💡 Why This Matters
🌍 Real World
Regex patterns help find words or patterns in text, useful in search tools, text editors, and data validation.
💼 Career
Knowing regex literal syntax is important for developers working with text processing, data cleaning, and automation scripts.
Progress0 / 4 steps
1
Create a sentence string
Create a string variable called sentence and set it to the exact text: "The quick brown fox jumps over the lazy dog".
Ruby
Hint
Use double quotes to create the string exactly as shown.
2
Create a regex pattern to find the word 'fox'
Create a regex pattern called pattern using Ruby's regex literal syntax to match the word fox. Use the exact syntax: /fox/.
Ruby
Hint
Use slashes / around the word to create the regex pattern.
3
Check if the word 'fox' is in the sentence
Use the regex pattern pattern with the =~ operator to check if sentence contains the word fox. Store the result in a variable called result.
Ruby
Hint
The =~ operator returns the index of the match or nil if not found.
4
Print the result of the regex match
Print the value of the variable result using puts to show the position where fox is found in the sentence.
Ruby
Hint
Use puts result to print the index where 'fox' starts.
Practice
(1/5)
1. What is the correct way to write a regex literal in Ruby to match the word cat?
easy
A. regex('cat')
B. "cat"
C. 'cat'
D. /cat/
Solution
Step 1: Understand regex literal syntax
In Ruby, regex literals are written between slashes, like /pattern/.
Step 2: Identify the correct pattern for 'cat'
The pattern to match the word 'cat' is simply /cat/.
Final Answer:
/cat/ -> Option D
Quick Check:
Regex literal = /pattern/ [OK]
Hint: Regex literals always use slashes /pattern/ in Ruby [OK]
Common Mistakes:
Using quotes instead of slashes
Calling a non-existent regex() function
Confusing string and regex syntax
2. Which of the following is the correct regex literal to match the word Dog case-insensitively in Ruby?
easy
A. /Dog/m
B. /Dog/g
C. /Dog/i
D. /Dog/
Solution
Step 1: Identify the case-insensitive option
In Ruby regex, the i option after the slashes makes matching case-insensitive.
Step 2: Check the options given
Only /Dog/i uses the correct i flag for case-insensitive matching.
Final Answer:
/Dog/i -> Option C
Quick Check:
Case-insensitive flag = i [OK]
Hint: Use /pattern/i for case-insensitive regex in Ruby [OK]
Common Mistakes:
Using g or m flags which mean different things
Omitting the i flag for case-insensitivity
Placing flags inside the slashes
3. What will be the output of this Ruby code?
text = "Hello World"
match = text =~ /world/i
puts match
medium
A. nil
B. 6
C. 0
D. Error
Solution
Step 1: Understand the =~ operator with regex
The operator =~ returns the index of the first match or nil if no match.
Step 2: Analyze the regex and string
The regex /world/i matches 'World' ignoring case. In "Hello World", 'World' starts at index 6 (0-based).
Final Answer:
6 -> Option B
Quick Check:
Case-insensitive match index = 6 [OK]
Hint: =~ returns match start index or nil if no match [OK]
Common Mistakes:
Ignoring case-insensitive flag i
Thinking output is true/false instead of index
Assuming match starts at 0 always
4. Identify the error in this Ruby regex literal:
pattern = /hello/i/
medium
A. Extra slash at the end causes syntax error
B. Missing quotes around pattern
C. Missing i flag for case-insensitivity
D. Regex literals cannot have flags
Solution
Step 1: Check regex literal syntax
A regex literal starts and ends with a single slash. Here, there is an extra slash at the end.
Step 2: Identify the syntax error
The extra slash after /hello/i is invalid and causes a syntax error.
Final Answer:
Extra slash at the end causes syntax error -> Option A
Quick Check:
Regex literal must start and end with one slash pair [OK]
Hint: Regex literals have exactly two slashes, no extras [OK]
Common Mistakes:
Adding extra slash after regex
Confusing regex with string quotes
Thinking flags go inside slashes
5. You want to find all words starting with a or A in a string using a regex literal. Which regex literal correctly matches this requirement?
hard
A. /\b[a]\w*/i
B. /\b[A]\w*/
C. /\b[a]\w*/
D. /\b[a-z]\w*/i
Solution
Step 1: Understand the requirement
We want words starting with 'a' or 'A'. Using case-insensitive flag i simplifies matching both.
Step 2: Analyze options
/\b[a]\w*/i uses /\b[a]\w*/i which matches word boundary, then 'a' (case-insensitive), then zero or more word chars. This matches words starting with 'a' or 'A'.
Final Answer:
/\b[a]\w*/i -> Option A
Quick Check:
Use i flag to match both 'a' and 'A' easily [OK]
Hint: Use /pattern/i to match letters ignoring case [OK]
Common Mistakes:
Listing both 'a' and 'A' inside brackets without i flag
Using wrong character classes
Missing word boundary \b
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.