www.tutorialspoint.com Forum Index
Register FAQMemberlistUsergroupsTutorials PointLog in
Reply to topic Page 1 of 1
Ruby On Rails Tutorial - regex
Author Message
Reply with quote
Post Ruby On Rails Tutorial - regex 
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:

Quote:
Example:

#!/usr/bin/ruby

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

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


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:


View user's profile Send private message
Display posts from previous:
Reply to topic Page 1 of 1
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum