Introduction
Regex helps you find and work with patterns in text easily. Ruby makes using regex simple and flexible.
Regex helps you find and work with patterns in text easily. Ruby makes using regex simple and flexible.
/pattern/flags
Patterns are written between slashes / /.
Flags like i make matching case-insensitive.
/cat/
/cat/i
/\d+/
This program checks if the text contains a phone number pattern like 123-456-7890. It prints a message if found.
text = "My phone number is 123-456-7890." if text =~ /\d{3}-\d{3}-\d{4}/ puts "Found a phone number!" else puts "No phone number found." end
Ruby lets you use regex directly with strings using operators like =~ or methods like match.
Regex can be combined with Ruby's string methods for powerful text processing.
Regex finds patterns in text quickly and easily.
Ruby's regex syntax is simple and flexible.
You can use regex to check, find, replace, or split text.
text = "hello123" match = text.match(/\d+/) puts match[0]
text = "abc123" if text =~ /\d+/ puts "Digits found" else puts "No digits" end
Pass the test and this pattern moves unknown