# Extracting range of lines

**URL:** <https://rubytalk.org/t/extracting-range-of-lines/47717>\
**Category:** ruby-talk\
**Created:** [15 July 2008 11:05 UTC](https://rubytalk.org/t/extracting-range-of-lines/47717 "2008-07-15T11:05:22Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![tmarc](https://avatars.discourse-cdn.com/v4/letter/t/4491bb/32.png) [@tmarc](https://rubytalk.org/u/tmarc)\
**Post date:** [15 July 2008 11:05 UTC](https://rubytalk.org/t/extracting-range-of-lines/47717/1 "2008-07-15T11:05:22Z")

</div>

Hello,

are there a ruby equivalents to perl's /regex/ .. /regex/ and /  
regex/ ... /regex/ idioms, examples described at  
[http://www.unix.com.ua/orelly/perl/cookbook/ch06\_09.htm](http://www.unix.com.ua/orelly/perl/cookbook/ch06_09.htm)?

I've been googling around with no relevant results.

Thanks in advance,  
Tom

---

<div class="post-metadata">

**Author:** ![smn](https://avatars.discourse-cdn.com/v4/letter/s/c89c15/32.png) [@smn](https://rubytalk.org/u/smn)\
**Post date:** [15 July 2008 11:40 UTC](https://rubytalk.org/t/extracting-range-of-lines/47717/2 "2008-07-15T11:40:30Z")

</div>

the link is not working (i would need to sign up to the forum  
first...)

> **···**
>
> On Jul 15, 1:05 pm, tmarc \<tmarcin...@gmail.com\> wrote:
> 
> > Hello,
> > 
> > are there a ruby equivalents to perl's /regex/ .. /regex/ and /  
> > regex/ ... /regex/ idioms, examples described athttp://www.unix.com.ua/orelly/perl/cookbook/ch06\_09.htm?
> > 
> > I've been googling around with no relevant results.
> > 
> > Thanks in advance,  
> > Tom

---

<div class="post-metadata">

**Author:** ![\_Pena\_Botp1](https://avatars.discourse-cdn.com/v4/letter/_/94ad74/32.png) [@\_Pena\_Botp1](https://rubytalk.org/u/_Pena_Botp1)\
**Post date:** [15 July 2008 11:54 UTC](https://rubytalk.org/t/extracting-range-of-lines/47717/3 "2008-07-15T11:54:55Z")

</div>

# are there a ruby equivalents to perl's /regex/ .. /regex/ and /  
# regex/ ... /regex/ idioms, examples described at  
# [http://www.unix.com.ua/orelly/perl/cookbook/ch06\_09.htm](http://www.unix.com.ua/orelly/perl/cookbook/ch06_09.htm)?  
# I've been googling around with no relevant results.

try searching for ruby+range+flip+flop

iianm, \*that\* perl/sed style is being deprecated.

eg,

&nbsp;&nbsp;File.open("test.txt") do |f|  
&nbsp;&nbsp;&nbsp;&nbsp;while line=f.gets  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;p line  
&nbsp;&nbsp;&nbsp;&nbsp;end  
&nbsp;&nbsp;end  
&nbsp;&nbsp;"--first\n"  
&nbsp;&nbsp;"1234\n"  
&nbsp;&nbsp;"456\n"  
&nbsp;&nbsp;"4321\n"  
&nbsp;&nbsp;"654\n"  
&nbsp;&nbsp;"--second\n"  
&nbsp;&nbsp;"546\n"  
&nbsp;&nbsp;"3456\n"  
&nbsp;&nbsp;"5436\n"  
&nbsp;&nbsp;"9879\n"  
&nbsp;&nbsp;"--third\n"  
&nbsp;&nbsp;"1111\n"  
&nbsp;&nbsp;#=\> nil

&nbsp;&nbsp;File.open("test.txt") do |f|  
&nbsp;&nbsp;&nbsp;&nbsp;while line=f.gets  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;p line if line =~ /^--second/ .. /^--third/  
&nbsp;&nbsp;&nbsp;&nbsp;end  
&nbsp;&nbsp;end  
&nbsp;&nbsp;"--second\n"  
&nbsp;&nbsp;#=\> nil

so you get weird results.

the new ruby syntax is like,

&nbsp;&nbsp;File.open("test.txt") do |f|  
&nbsp;&nbsp;&nbsp;&nbsp;while line=f.gets  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;p line if line =~ /^--second/ .. line =~ /^--third/  
&nbsp;&nbsp;&nbsp;&nbsp;end  
&nbsp;&nbsp;end  
&nbsp;&nbsp;"--second\n"  
&nbsp;&nbsp;"546\n"  
&nbsp;&nbsp;"3456\n"  
&nbsp;&nbsp;"5436\n"  
&nbsp;&nbsp;"9879\n"  
&nbsp;&nbsp;"--third\n"  
&nbsp;&nbsp;#=\> nil

hth.  
kind regards -botp

> **···**
>
> From: tmarc [[mailto:tmarciniak@gmail.com](mailto:tmarciniak@gmail.com)]
