From: "Jesús Gabriel y Galán" Date: 2011-04-27T17:52:17+09:00 Subject: Re: pattern matching and array methods On Wed, Apr 27, 2011 at 9:57 AM, Brian Candler wrote: > Dhruva Sagar wrote in post #995263: >> Something like this should do it : >> >> line.each {|l| puts l if l =~ /^(1:2)|(1:3)|(1:4)/} > > That's a poor answer, because your regexp isn't anchored properly. It > would match "5:6 abc1:3def" and "1:23 foobar" for example. > > I suggest using the regexp to parse the line, then using numeric > testing. This makes it easier to solve the other example of 3:9 to 3:31 > > lines.each do |line| >  if line =~ /^(\d+):(\d+)/ >    major, minor = $1.to_i, $2.to_i >    puts line if major == 3 and (9..31).include?(minor) >  end > end Generalizing a bit more: lower_major, lower_minor = "3:9".split(":").map {|x| x.to_i} upper_major, upper_minor = "3:31".split(":").map {|x| x.to_i} major_range = lower_major..upper_major minor_range = lower_minor..upper_minor lines.each do |line| if line =~ /^(\d+):(\d+)/ major, minor = $1.to_i, $2.to_i puts line if major_range.include?(major) and minor_range.include?(minor) end end Jesus.