Introduction
We use common patterns and character classes to find or check specific types of characters in text easily.
We use common patterns and character classes to find or check specific types of characters in text easily.
/[pattern]/
Patterns go inside slashes / / in Ruby.
Character classes like \d or \w match groups of characters.
/\d/
/\w+/
/[aeiou]/
/[^0-9]/
This program looks inside the text "Hello123" to find digits and words using common patterns.
text = "Hello123" puts "Digits found:" text.scan(/\d/).each { |d| puts d } puts "Words found:" text.scan(/\w+/).each { |w| puts w }
Use double backslashes \\ in Ruby strings to write special patterns like \d.
Character classes help you avoid writing many separate characters.
Square brackets [] define a set of characters to match.
Common patterns help find specific characters in text.
Character classes like \d and \w match digits and words.
Use these patterns inside slashes / / in Ruby.
text = "Room 101" match = text.match(/\d+/) puts match[0]
email = "user@example.com" if email =~ /\w+@\w+.\w+/ puts "Valid email" else puts "Invalid email" end
"Hello World from Ruby". Which Ruby regex pattern should you use with scan?Pass the test and this pattern moves unknown