0
0
Rubyprogramming~15 mins

Why regex is powerful in Ruby - See It in Action

Choose your learning style9 modes available
Why regex is powerful in Ruby
📖 Scenario: You are working on a simple text processing task where you need to find and extract specific patterns from a string. Ruby's regular expressions (regex) make this easy and powerful.
🎯 Goal: Learn how to use Ruby regex to find all words that start with a capital letter in a sentence.
📋 What You'll Learn
Create a string variable with a sentence containing several words, some starting with capital letters
Create a regex pattern to match words starting with a capital letter
Use Ruby's regex methods to find all matching words
Print the list of matched words
💡 Why This Matters
🌍 Real World
Regex is used in many real-world tasks like searching text, validating input, and extracting data from documents.
💼 Career
Knowing regex in Ruby is valuable for jobs in web development, data processing, and automation where text handling is common.
Progress0 / 4 steps
1
Create the sentence string
Create a string variable called sentence and set it to the exact value: "Ruby is Powerful and Easy to Learn"
Ruby
Need a hint?

Use double quotes to create the string exactly as shown.

2
Create the regex pattern
Create a regex variable called capital_word_pattern that matches words starting with a capital letter. Use the pattern /\b[A-Z][a-z]*\b/
Ruby
Need a hint?

The pattern matches word boundaries, a capital letter, then zero or more lowercase letters.

3
Find all matching words
Use the scan method on sentence with capital_word_pattern to find all words starting with a capital letter. Store the result in a variable called capital_words
Ruby
Need a hint?

The scan method returns an array of all matches.

4
Print the matched words
Print the variable capital_words to display the list of words starting with a capital letter
Ruby
Need a hint?

Use puts to print the array. Each word will appear on its own line.