Discover how a few symbols can save you hours of tedious searching!
Why Common patterns and character classes in Ruby? - Purpose & Use Cases
Imagine you need to find all phone numbers or email addresses in a long text by checking each character one by one.
Doing this by hand is slow and tiring. You might miss some patterns or make mistakes because the rules are tricky and many characters look similar.
Using common patterns and character classes lets you describe groups of characters easily. This way, Ruby can quickly find what you want without checking every single character manually.
text.each_char { |c| check if c is digit or letter }text.scan(/\d{3}-\d{3}-\d{4}/)You can quickly search and match complex text patterns with simple, readable rules.
Extracting all phone numbers from customer messages to contact them faster.
Manual character checks are slow and error-prone.
Character classes group similar characters for easy matching.
Common patterns let Ruby find text quickly and accurately.
Practice
Solution
Step 1: Understand \d in Ruby regex
The pattern \d matches any digit character (0-9).Step 2: Compare with other options
\w matches word characters (letters, digits, underscore), \s matches whitespace, \D matches non-digit characters.Final Answer:
/\d/ -> Option BQuick Check:
Digit pattern = /\d/ [OK]
- Confusing \d with \w
- Using /\D/ which matches non-digits
- Thinking \s matches digits
Solution
Step 1: Understand quantifiers in regex
+ means one or more times, * means zero or more, ? means zero or one.Step 2: Apply to \w
/\w+/ matches one or more word characters, which is the correct syntax.Final Answer:
/\w+/ -> Option AQuick Check:
One or more word chars = /\w+/ [OK]
- Using * which allows zero matches
- Using ? which allows zero or one match
- Using empty braces {} which is invalid
text = "Room 101" match = text.match(/\d+/) puts match[0]
Solution
Step 1: Understand the regex /\d+/
This pattern matches one or more digits in a row.Step 2: Apply match to string "Room 101"
The digits "101" are matched as a group, so match[0] returns "101".Final Answer:
101 -> Option AQuick Check:
Digits matched = 101 [OK]
- Thinking it matches only one digit
- Confusing match[0] with match object
- Expecting the word part instead of digits
email = "user@example.com" if email =~ /\w+@\w+.\w+/ puts "Valid email" else puts "Invalid email" end
Solution
Step 1: Examine the regex pattern
The pattern /\w+@\w+.\w+/ uses an unescaped . which matches any character, not a literal dot.Step 2: Identify the error
The dot after the domain must be escaped as \. to match the literal "." in email addresses.Final Answer:
The regex does not escape the dot properly -> Option CQuick Check:
Escape literal dot as \. [OK]
- Thinking dot doesn't need escaping
- Misusing =~ operator
- Confusing \w with \d in email regex
"Hello World from Ruby". Which Ruby regex pattern should you use with scan?Solution
Step 1: Understand the requirement
We want words starting with a capital letter, so first letter must be A-Z.Step 2: Analyze the regex options
/\b[A-Z]\w*/ matches word boundary, then capital letter, then zero or more word chars.Step 3: Check other options
/\b[a-z]\w*/ matches lowercase start, /\d+/ matches digits, /\s+/ matches spaces, so they don't fit.Final Answer:
/\b[A-Z]\w*/ -> Option DQuick Check:
Capital-start words = /\b[A-Z]\w*/ [OK]
- Using [a-z] instead of [A-Z]
- Forgetting word boundary \b
- Using digit or space patterns incorrectly
Ready to prove it?
Pass the test and this pattern moves unknown
