0
0
Rubyprogramming~15 mins

Regex literal syntax (/pattern/) in Ruby - Mini Project: Build & Apply

Choose your learning style9 modes available
Using Regex Literal Syntax in Ruby
📖 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
Need a 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
Need a 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
Need a 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
Need a hint?

Use puts result to print the index where 'fox' starts.