February 2, 2004 Intro to Ruby Slide #30
Prev Next

Regular expressions

The syntax for regular expressions is pretty similar to Perl:
     s = "I like apple pie"
     if s =~ /like (.*) pie/
       fruit = $1  # == "apple"
     end

However, substitution is done by calling methods in the String class:
     s.sub!("apple", "cherry")
     s.gsub!("e", "E")
     s.gsub!(/[aeiou]/){|s| s.upcase}


Prev Copyright © 2004 Walter C. Mankowski Next