Ruby On Rails Tutorial - regex

Postby yertle » Wed May 26, 2010 9:32 pm

Posts: 1
Joined: Wed May 26, 2010 9:27 pm
I stumbled on the regular expressions part of the Ruby Regular Expressions tutorial, and the very first example doesn't make very much sense to me. Here it is:



line1 = "Cats are smarter than dogs";
line2 = "Dogs also like meat";

Code: Select all
if ( line1 =~ /Cats(.*)/ )
  puts "Line1 starts with Cats"
if ( line2 =~ /Cats(.*)/ )
  puts "Line2 starts with Dogs"

This will produce following result:

Line starts with Cats

First of all there's no way that I see to print "Line starts with Cats" - it should be "Line1 starts with Cats"

Second of all, it should be printing "Line2 starts with Dogs" - because line2 =~ /Cats(.*)/

What am I missing? I'm hoping I'm in fact understanding how regular expressions work and that the example has some issues. It's confusing for the first example to potentially be wrong though... Any help would be appreciated, thanks.
Code: Select all