On Wed, Apr 27, 2011 at 9:57 AM, Brian Candler <b.candler / pobox.com> wrote:
> Dhruva Sagar wrote in post #995263:
>> Something like this should do it :
>>
>> line.each {|l| puts l if l =3D~ /^(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|
> =A0if line =3D~ /^(\d+):(\d+)/
> =A0 =A0major, minor =3D $1.to_i, $2.to_i
> =A0 =A0puts line if major =3D=3D 3 and (9..31).include?(minor)
> =A0end
> end

Generalizing a bit more:

lower_major, lower_minor =3D "3:9".split(":").map {|x| x.to_i}
upper_major, upper_minor =3D "3:31".split(":").map {|x| x.to_i}
major_range =3D lower_major..upper_major
minor_range =3D lower_minor..upper_minor

lines.each do |line|
  if line =3D~ /^(\d+):(\d+)/
   major, minor =3D $1.to_i, $2.to_i
   puts line if major_range.include?(major) and minor_range.include?(minor)
 end
end

Jesus.