Regular Expressions
=~ and match test patterns. scan finds all matches and gsub replaces.
Example
s = "Call 555-1234 or 555-9876"
puts s =~ /\d{3}-\d{4}/
puts s.scan(/\d{3}-\d{4}/).inspect
m = s.match(/(\d{3})-(\d{4})/)
puts m[1], m[2]
puts s.gsub(/\d/, "#")Result
5 ["555-1234", "555-9876"] 555 1234 Call ###-#### or ###-####
Exercise
Find all matches.
s.(/a/)
Quiz Test
These free lessons are for learning. Join a full course for projects, mentor feedback and a verified certificate.