# Knocking Lines Out Of A Multiline String

**URL:** https://rubytalk.org/t/knocking-lines-out-of-a-multiline-string/36361
**Category:** ruby-talk
**Created:** [22 March 2007 14:43 UTC](https://rubytalk.org/t/knocking-lines-out-of-a-multiline-string/36361 "2007-03-22T14:43:15Z")
**Posts on this page:** 19
**Page:** 1

<div class="post-metadata">

### Author: ![Andrew\_Stewart](https://avatars.discourse-cdn.com/v4/letter/a/9de053/32.png) [@Andrew\_Stewart](https://rubytalk.org/u/Andrew_Stewart)
#### Post date: [22 March 2007 14:43 UTC](https://rubytalk.org/t/knocking-lines-out-of-a-multiline-string/36361/1 "2007-03-22T14:43:15Z")

</div>

Hello,

What's a (good!) way to remove lines matching a pattern from a multiline string?

For example, I would like to remove lines matching /usr/local/lib from the multiline string:

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;/usr/local/lib/ruby/gems/1.8/gems/actionpack-1.13.1/lib/action\_controller/test\_process.rb:382:in `process'  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;/usr/local/lib/ruby/gems/1.8/gems/actionpack-1.13.1/lib/action\_controller/test\_process.rb:353:in `post'  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;test/functional/orders\_controller\_test.rb:241:in `test\_should\_handle\_errors\_on\_edit'

...to give:

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;test/functional/orders\_controller\_test.rb:241:in `test\_should\_handle\_errors\_on\_edit'

I tried matching the pattern-to-remove with gsub and substituting an empty string, but that leaves me with lots of blank lines and not really any nearer to the answer.

Thanks and regards,  
Andy Stewart

---

<div class="post-metadata">

### Author: ![Robert\_K1](https://yyz1.discourse-cdn.com/flex029/user_avatar/rubytalk.org/robert_k1/32/1830_2.png) [@Robert\_K1](https://rubytalk.org/u/Robert_K1)
#### Post date: [22 March 2007 15:10 UTC](https://rubytalk.org/t/knocking-lines-out-of-a-multiline-string/36361/2 "2007-03-22T15:10:05Z")

</div>

> What's a (good!) way to remove lines matching a pattern from a multiline string?
> 
> For example, I would like to remove lines matching /usr/local/lib from the multiline string:
> 
> &nbsp;&nbsp;&nbsp;&nbsp;/usr/local/lib/ruby/gems/1.8/gems/actionpack-1.13.1/lib/action\_controller/test\_process.rb:382:in `process'  
> &nbsp;&nbsp;&nbsp;&nbsp;/usr/local/lib/ruby/gems/1.8/gems/actionpack-1.13.1/lib/action\_controller/test\_process.rb:353:in `post'  
> &nbsp;&nbsp;&nbsp;&nbsp;test/functional/orders\_controller\_test.rb:241:in `test\_should\_handle\_errors\_on\_edit'
> 
> ..to give:
> 
> &nbsp;&nbsp;&nbsp;&nbsp;test/functional/orders\_controller\_test.rb:241:in `test\_should\_handle\_errors\_on\_edit'
> 
> I tried matching the pattern-to-remove with gsub and substituting an empty string, but that leaves me with lots of blank lines and not really any nearer to the answer.

Convert it to an array and select like

\>\> "foo\nbar\n".to\_a.select {|l| /^f/ =~ l}  
=\> ["foo\n"]

Kind regards

&nbsp;&nbsp;robert

> **···**
>
> On 22.03.2007 15:43, Andrew Stewart wrote:

---

<div class="post-metadata">

### Author: ![Leslie\_Viljoen1](https://avatars.discourse-cdn.com/v4/letter/l/f1d935/32.png) [@Leslie\_Viljoen1](https://rubytalk.org/u/Leslie_Viljoen1)
#### Post date: [22 March 2007 15:15 UTC](https://rubytalk.org/t/knocking-lines-out-of-a-multiline-string/36361/3 "2007-03-22T15:15:45Z")

</div>

You could change your lines into an array of lines and then remove the  
lines that match:

lines =   
File.new("text.txt").read.each\_line {|line| lines \<\< line }  
lines.delete\_if {|line| line =~ /\/usr\/local\/lib/}

> **···**
>
> On 3/22/07, Andrew Stewart \<boss@airbladesoftware.com\> wrote:
> 
> > Hello,
> > 
> > What's a (good!) way to remove lines matching a pattern from a  
> > multiline string?
> > 
> > For example, I would like to remove lines matching /usr/local/lib  
> > from the multiline string:
> > 
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;/usr/local/lib/ruby/gems/1.8/gems/actionpack-1.13.1/lib/  
> > action\_controller/test\_process.rb:382:in `process'  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;/usr/local/lib/ruby/gems/1.8/gems/actionpack-1.13.1/lib/  
> > action\_controller/test\_process.rb:353:in `post'  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;test/functional/orders\_controller\_test.rb:241:in  
> > `test\_should\_handle\_errors\_on\_edit'
> > 
> > ...to give:
> > 
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;test/functional/orders\_controller\_test.rb:241:in  
> > `test\_should\_handle\_errors\_on\_edit'
> > 
> > I tried matching the pattern-to-remove with gsub and substituting an  
> > empty string, but that leaves me with lots of blank lines and not  
> > really any nearer to the answer.
> 
> --  
> If you could create a machine that copies hamburgers — you put one  
> hamburger in and two equally good hamburgers come out the other side —  
> it would be unethical not to do so and make it freely available.

---

<div class="post-metadata">

### Author: ![Rob\_Biedenharn1](https://avatars.discourse-cdn.com/v4/letter/r/57b2e6/32.png) [@Rob\_Biedenharn1](https://rubytalk.org/u/Rob_Biedenharn1)
#### Post date: [22 March 2007 15:22 UTC](https://rubytalk.org/t/knocking-lines-out-of-a-multiline-string/36361/4 "2007-03-22T15:22:17Z")

</div>

> Hello,
> 
> What's a (good!) way to remove lines matching a pattern from a multiline string?
> 
> For example, I would like to remove lines matching /usr/local/lib from the multiline string:
> 
> &nbsp;&nbsp;&nbsp;&nbsp;/usr/local/lib/ruby/gems/1.8/gems/actionpack-1.13.1/lib/action\_controller/test\_process.rb:382:in `process'  
> &nbsp;&nbsp;&nbsp;&nbsp;/usr/local/lib/ruby/gems/1.8/gems/actionpack-1.13.1/lib/action\_controller/test\_process.rb:353:in `post'  
> &nbsp;&nbsp;&nbsp;&nbsp;test/functional/orders\_controller\_test.rb:241:in `test\_should\_handle\_errors\_on\_edit'
> 
> ...to give:
> 
> &nbsp;&nbsp;&nbsp;&nbsp;test/functional/orders\_controller\_test.rb:241:in `test\_should\_handle\_errors\_on\_edit'
> 
> I tried matching the pattern-to-remove with gsub and substituting an empty string, but that leaves me with lots of blank lines and not really any nearer to the answer.
> 
> Thanks and regards,  
> Andy Stewart

\>\> input = " /usr/local/lib/ruby/gems/1.8/gems/actionpack-1.13.1/lib/action\_controller/test\_process.rb:382:in `process'  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;/usr/local/lib/ruby/gems/1.8/gems/actionpack-1.13.1/lib/action\_controller/test\_process.rb:353:in `post'  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;test/functional/orders\_controller\_test.rb:241:in `test\_should\_handle\_errors\_on\_edit'  
"  
=\> " /usr/local/lib/ruby/gems/1.8/gems/actionpack-1.13.1/lib/action\_controller/test\_process.rb:382:in `process'\n /usr/local/lib/ruby/gems/1.8/gems/actionpack-1.13.1/lib/action_controller/test_process.rb:353:in `post'\n test/functional/orders\_controller\_test.rb:241:in `test\_should\_handle\_errors\_on\_edit'\n"

\>\> input.gsub(%r{^.\*/usr/local/lib/.\*\n?},'')  
=\> " test/functional/orders\_controller\_test.rb:241:in `test\_should\_handle\_errors\_on\_edit'\n"

If you showed your code, an explanation could be added as to your regexp, but the concept certainly works as I've shown.

-Rob

Rob Biedenharn [http://agileconsultingllc.com](http://agileconsultingllc.com)  
Rob@AgileConsultingLLC.com

> **···**
>
> On Mar 22, 2007, at 10:43 AM, Andrew Stewart wrote:

---

<div class="post-metadata">

### Author: ![Robert\_K1](https://yyz1.discourse-cdn.com/flex029/user_avatar/rubytalk.org/robert_k1/32/1830_2.png) [@Robert\_K1](https://rubytalk.org/u/Robert_K1)
#### Post date: [22 March 2007 15:15 UTC](https://rubytalk.org/t/knocking-lines-out-of-a-multiline-string/36361/5 "2007-03-22T15:15:04Z")

</div>

> > What's a (good!) way to remove lines matching a pattern from a multiline string?
> > 
> > For example, I would like to remove lines matching /usr/local/lib from the multiline string:
> > 
> > &nbsp;&nbsp;&nbsp;&nbsp;/usr/local/lib/ruby/gems/1.8/gems/actionpack-1.13.1/lib/action\_controller/test\_process.rb:382:in `process'  
> > &nbsp;&nbsp;&nbsp;&nbsp;/usr/local/lib/ruby/gems/1.8/gems/actionpack-1.13.1/lib/action\_controller/test\_process.rb:353:in `post'  
> > &nbsp;&nbsp;&nbsp;&nbsp;test/functional/orders\_controller\_test.rb:241:in `test\_should\_handle\_errors\_on\_edit'
> > 
> > ..to give:
> > 
> > &nbsp;&nbsp;&nbsp;&nbsp;test/functional/orders\_controller\_test.rb:241:in `test\_should\_handle\_errors\_on\_edit'
> > 
> > I tried matching the pattern-to-remove with gsub and substituting an empty string, but that leaves me with lots of blank lines and not really any nearer to the answer.
> 
> Convert it to an array and select like
> 
> \>\> "foo\nbar\n".to\_a.select {|l| /^f/ =~ l}  
> =\> ["foo\n"]

Bullshit: just use select:

\>\> "foo\nbar\n".select {|l| /^f/ =~ l}  
=\> ["foo\n"]

Sorry for the noise.

&nbsp;&nbsp;robert

> **···**
>
> On 22.03.2007 16:09, Robert Klemme wrote:
> 
> > On 22.03.2007 15:43, Andrew Stewart wrote:

---

<div class="post-metadata">

### Author: ![Gavin\_Kistner2](https://avatars.discourse-cdn.com/v4/letter/g/b9e5f3/32.png) [@Gavin\_Kistner2](https://rubytalk.org/u/Gavin_Kistner2)
#### Post date: [22 March 2007 15:45 UTC](https://rubytalk.org/t/knocking-lines-out-of-a-multiline-string/36361/6 "2007-03-22T15:45:07Z")

</div>

Leslie, as a public service announcement, you should be aware of  
IO.readlines:

C:\\>qri IO.readlines

> **···**
>
> On Mar 22, 9:15 am, "Leslie Viljoen" \<leslievilj...@gmail.com\> wrote:
> 
> > You could change your lines into an array of lines and then remove the  
> > lines that match:
> > 
> > lines =   
> > File.new("text.txt").read.each\_line {|line| lines \<\< line }  
> > lines.delete\_if {|line| line =~ /\/usr\/local\/lib/}
> 
> ----------------------------------------------------------  
> IO::readlines  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;IO.readlines(name, sep\_string=$/) =\> array  
> ------------------------------------------------------------------------  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Reads the entire file specified by \_name\_ as individual lines,  
> and  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;returns those lines in an array. Lines are separated by  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\_sep\_string\_.
> 
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;a = IO.readlines("testfile")  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;a[0] #=\> "This is line one\n"
> 
> For that matter, you should also be aware of IO.read:  
> ---------------------------------------------------------------  
> IO::read  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;IO.read(name, [length [, offset]] ) =\> string  
> ------------------------------------------------------------------------  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Opens the file, optionally seeks to the given offset, then  
> returns  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\_length\_ bytes (defaulting to the rest of the file). +read+  
> ensures  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;the file is closed before returning.
> 
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;IO.read("testfile") #=\> "This is line one\nThis is  
> line two\nThis is line three\nAnd so on...\n"  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;IO.read("testfile", 20) #=\> "This is line one\nThi"  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;IO.read("testfile", 20, 10) #=\> "ne one\nThis is line "
> 
> You should also be aware of the block form of #open, which opens the  
> IO object and then closes it when done.
> 
> What you wrote creates a new File object and opens it, but never  
> closes it. I'm not really sure what badness can result from this, but  
> I gather it's not a good idea.

---

<div class="post-metadata">

### Author: ![Andrew\_Stewart](https://avatars.discourse-cdn.com/v4/letter/a/9de053/32.png) [@Andrew\_Stewart](https://rubytalk.org/u/Andrew_Stewart)
#### Post date: [22 March 2007 16:48 UTC](https://rubytalk.org/t/knocking-lines-out-of-a-multiline-string/36361/7 "2007-03-22T16:48:14Z")

</div>

Leslie, thanks for that. That works for me (with a join chained on the end).

Regards,  
Andy Stewart

> **···**
>
> On 22 Mar 2007, at 15:15, Leslie Viljoen wrote:
> 
> > lines.delete\_if {|line| line =~ /\/usr\/local\/lib/}

---

<div class="post-metadata">

### Author: ![Andrew\_Stewart](https://avatars.discourse-cdn.com/v4/letter/a/9de053/32.png) [@Andrew\_Stewart](https://rubytalk.org/u/Andrew_Stewart)
#### Post date: [22 March 2007 16:52 UTC](https://rubytalk.org/t/knocking-lines-out-of-a-multiline-string/36361/8 "2007-03-22T16:52:32Z")

</div>

Aha! You have proved that I chose my regexp poorly. Here's what I tried:

input.gsub(%r{^.\*/usr/local/lib/.\*$}i, '')

The difference is that yours consumes the new line character but mine doesn't. I should have just matched it explicitly like you rather than using an anchor.

Thanks and regards,  
Andy Stewart

> **···**
>
> On 22 Mar 2007, at 15:22, Rob Biedenharn wrote:
> 
> > \>\> input.gsub(%r{^.\*/usr/local/lib/.\*\n?},'')
> > 
> > If you showed your code, an explanation could be added as to your regexp, but the concept certainly works as I've shown.

---

<div class="post-metadata">

### Author: ![Andrew\_Stewart](https://avatars.discourse-cdn.com/v4/letter/a/9de053/32.png) [@Andrew\_Stewart](https://rubytalk.org/u/Andrew_Stewart)
#### Post date: [22 March 2007 16:47 UTC](https://rubytalk.org/t/knocking-lines-out-of-a-multiline-string/36361/9 "2007-03-22T16:47:30Z")

</div>

Thanks for that. So simple once you've seen it.

Regards,  
Andy Stewart

> **···**
>
> On 22 Mar 2007, at 15:15, Robert Klemme wrote:
> 
> > \>\> "foo\nbar\n".select {|l| /^f/ =~ l}  
> > =\> ["foo\n"]

---

<div class="post-metadata">

### Author: ![Leslie\_Viljoen1](https://avatars.discourse-cdn.com/v4/letter/l/f1d935/32.png) [@Leslie\_Viljoen1](https://rubytalk.org/u/Leslie_Viljoen1)
#### Post date: [22 March 2007 20:56 UTC](https://rubytalk.org/t/knocking-lines-out-of-a-multiline-string/36361/10 "2007-03-22T20:56:48Z")

</div>

This does sound rather frightening! What \*is\* the effect of opening a  
file and not closing it?  
😉

Also, doesn't the above say that IO.read closes the file afterwards?

> **···**
>
> On 3/22/07, Phrogz \<gavin@refinery.com\> wrote:
> 
> > On Mar 22, 9:15 am, "Leslie Viljoen" \<leslievilj...@gmail.com\> wrote:  
> > \> You could change your lines into an array of lines and then remove the  
> > \> lines that match:  
> > \>  
> > \> lines =   
> > \> File.new("text.txt").read.each\_line {|line| lines \<\< line }  
> > \> lines.delete\_if {|line| line =~ /\/usr\/local\/lib/}
> > 
> > Leslie, as a public service announcement, you should be aware of  
> > IO.readlines:
> > 
> > C:\\>qri IO.readlines  
> > ----------------------------------------------------------  
> > IO::readlines  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;IO.readlines(name, sep\_string=$/) =\> array  
> > ------------------------------------------------------------------------  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Reads the entire file specified by \_name\_ as individual lines,  
> > and  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;returns those lines in an array. Lines are separated by  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\_sep\_string\_.
> > 
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;a = IO.readlines("testfile")  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;a[0] #=\> "This is line one\n"
> > 
> > For that matter, you should also be aware of IO.read:  
> > ---------------------------------------------------------------  
> > IO::read  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;IO.read(name, [length [, offset]] ) =\> string  
> > ------------------------------------------------------------------------  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Opens the file, optionally seeks to the given offset, then  
> > returns  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\_length\_ bytes (defaulting to the rest of the file). +read+  
> > ensures  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;the file is closed before returning.
> > 
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;IO.read("testfile") #=\> "This is line one\nThis is  
> > line two\nThis is line three\nAnd so on...\n"  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;IO.read("testfile", 20) #=\> "This is line one\nThi"  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;IO.read("testfile", 20, 10) #=\> "ne one\nThis is line "
> > 
> > You should also be aware of the block form of #open, which opens the  
> > IO object and then closes it when done.
> > 
> > What you wrote creates a new File object and opens it, but never  
> > closes it. I'm not really sure what badness can result from this, but  
> > I gather it's not a good idea.
> 
> --  
> If you could create a machine that copies hamburgers — you put one  
> hamburger in and two equally good hamburgers come out the other side —  
> it would be unethical not to do so and make it freely available.

---

<div class="post-metadata">

### Author: ![Rick\_DeNatale1](https://avatars.discourse-cdn.com/v4/letter/r/bbce88/32.png) [@Rick\_DeNatale1](https://rubytalk.org/u/Rick_DeNatale1)
#### Post date: [23 March 2007 18:39 UTC](https://rubytalk.org/t/knocking-lines-out-of-a-multiline-string/36361/11 "2007-03-23T18:39:42Z")

</div>

Or, since he's really trying to exclude lines which include /usr/local/lib:

str.reject {|s| Regexp.new("/usr/local/lib").match(s)}

> **···**
>
> On 3/22/07, Robert Klemme \<shortcutter@googlemail.com\> wrote:
> 
> > Bullshit: just use select:
> > 
> > \>\> "foo\nbar\n".select {|l| /^f/ =~ l}  
> > =\> ["foo\n"]
> 
> --  
> Rick DeNatale
> 
> My blog on Ruby  
> [http://talklikeaduck.denhaven2.com/](http://talklikeaduck.denhaven2.com/)

---

<div class="post-metadata">

### Author: ![Gavin\_Kistner2](https://avatars.discourse-cdn.com/v4/letter/g/b9e5f3/32.png) [@Gavin\_Kistner2](https://rubytalk.org/u/Gavin_Kistner2)
#### Post date: [22 March 2007 21:45 UTC](https://rubytalk.org/t/knocking-lines-out-of-a-multiline-string/36361/12 "2007-03-22T21:45:11Z")

</div>

> \> \> lines =   
> \> \> File.new("text.txt").read.each\_line {|line| lines \<\< line }  
> \> \> lines.delete\_if {|line| line =~ /\/usr\/local\/lib/}

[snip]

> \> What you wrote creates a new File object and opens it, but never  
> \> closes it. I'm not really sure what badness can result from this, but  
> \> I gather it's not a good idea.
> 
> This does sound rather frightening! What \*is\* the effect of opening a  
> file and not closing it?  
> 😉
> 
> Also, doesn't the above say that IO.read closes the file afterwards?

IO.read (the class method) does open/close the file, but IO#read (the  
instance method) does not. Manually managing an IO object, you need  
something like:  
&nbsp;&nbsp;f = File.new('foo.txt')  
&nbsp;&nbsp;f.read  
&nbsp;&nbsp;f.close

> **···**
>
> On Mar 22, 2:56 pm, "Leslie Viljoen" \<leslievilj...@gmail.com\> wrote:
> 
> > On 3/22/07, Phrogz \<g...@refinery.com\> wrote:  
> > \> On Mar 22, 9:15 am, "Leslie Viljoen" \<leslievilj...@gmail.com\> wrote:

---

<div class="post-metadata">

### Author: ![Brian\_Candler](https://avatars.discourse-cdn.com/v4/letter/b/5f9b8f/32.png) [@Brian\_Candler](https://rubytalk.org/u/Brian_Candler)
#### Post date: [23 March 2007 20:34 UTC](https://rubytalk.org/t/knocking-lines-out-of-a-multiline-string/36361/13 "2007-03-23T20:34:29Z")

</div>

Why does nobody seem to anchor regexps? I have seen so many documentation  
examples now which suggest that something like

&nbsp;&nbsp;&nbsp;&nbsp;raise "Invalid data" unless /[A-Za-z0-9]+/ =~ data

is a good example of data validation ☹

It would be a drift away from Perl, but I wonder if regexps should be  
anchored by default, and you'd have to add a flag to them to make them  
unanchored...

Sorry, just my gripe of the day.

Regards,

Brian.

> **···**
>
> On Sat, Mar 24, 2007 at 03:39:42AM +0900, Rick DeNatale wrote:
> 
> > On 3/22/07, Robert Klemme \<shortcutter@googlemail.com\> wrote:
> > 
> > \>Bullshit: just use select:  
> > \>  
> > \> \>\> "foo\nbar\n".select {|l| /^f/ =~ l}  
> > \>=\> ["foo\n"]
> > 
> > Or, since he's really trying to exclude lines which include /usr/local/lib:
> > 
> > str.reject {|s| Regexp.new("/usr/local/lib").match(s)}

---

<div class="post-metadata">

### Author: ![Gavin\_Kistner2](https://avatars.discourse-cdn.com/v4/letter/g/b9e5f3/32.png) [@Gavin\_Kistner2](https://rubytalk.org/u/Gavin_Kistner2)
#### Post date: [26 March 2007 15:30 UTC](https://rubytalk.org/t/knocking-lines-out-of-a-multiline-string/36361/14 "2007-03-26T15:30:06Z")

</div>

Note that the above results in Regexp#new being called for each line  
in the string; not very efficient:  
&nbsp;&nbsp;require 'Benchmark'

&nbsp;&nbsp;s = "foobar"  
&nbsp;&nbsp;N = 1000000

&nbsp;&nbsp;Benchmark.bmbm{ |x|  
&nbsp;&nbsp;&nbsp;&nbsp;x.report( 'Regexp.new' ){  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;N.times{ Regexp.new( "foobar" ) =~ s }  
&nbsp;&nbsp;&nbsp;&nbsp;}  
&nbsp;&nbsp;&nbsp;&nbsp;x.report( 'inline literal' ){  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;N.times{ /foobar/ =~ s }  
&nbsp;&nbsp;&nbsp;&nbsp;}  
&nbsp;&nbsp;&nbsp;&nbsp;x.report( 'as variable' ){  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;r = /foobar/  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;N.times{ r =~ s }  
&nbsp;&nbsp;&nbsp;&nbsp;}  
&nbsp;&nbsp;}

&nbsp;&nbsp;#=\> Rehearsal --------------------------------------------------  
&nbsp;&nbsp;#=\> Regexp.new 20.844000 1.875000 22.719000 ( 22.750000)  
&nbsp;&nbsp;#=\> inline literal 0.906000 0.000000 0.906000 ( 0.906000)  
&nbsp;&nbsp;#=\> as variable 1.094000 0.000000 1.094000 ( 1.094000)  
&nbsp;&nbsp;#=\> ---------------------------------------- total: 24.719000sec  
&nbsp;&nbsp;#=\>  
&nbsp;&nbsp;#=\> user system total real  
&nbsp;&nbsp;#=\> Regexp.new 21.234000 1.671000 22.905000 ( 22.922000)  
&nbsp;&nbsp;#=\> inline literal 0.891000 0.000000 0.891000 ( 0.891000)  
&nbsp;&nbsp;#=\> as variable 1.047000 0.000000 1.047000 ( 1.047000)

If you just want to avoid having to escape the forward slashes in the  
literal, you can use the %r notation for a regexp literal. For  
example:  
&nbsp;&nbsp;str.reject{ |s| %r{/usr/local/lib} =~ s }

> **···**
>
> On Mar 23, 12:39 pm, "Rick DeNatale" \<rick.denat...@gmail.com\> wrote:
> 
> > str.reject {|s| Regexp.new("/usr/local/lib").match(s)}

---

<div class="post-metadata">

### Author: ![Vince\_H\_K](https://avatars.discourse-cdn.com/v4/letter/v/c57346/32.png) [@Vince\_H\_K](https://rubytalk.org/u/Vince_H_K)
#### Post date: [23 March 2007 20:48 UTC](https://rubytalk.org/t/knocking-lines-out-of-a-multiline-string/36361/15 "2007-03-23T20:48:12Z")

</div>

Brian Candler wrote:

> > > Bullshit: just use select:
> > > 
> > > > > "foo\nbar\n".select {|l| /^f/ =~ l}
> > > 
> > > =\> ["foo\n"]
> > 
> > Or, since he's really trying to exclude lines which include /usr/local/lib:
> > 
> > str.reject {|s| Regexp.new("/usr/local/lib").match(s)}
> 
> Why does nobody seem to anchor regexps? I have seen so many documentation  
> examples now which suggest that something like
> 
> &nbsp;&nbsp;&nbsp;&nbsp;raise "Invalid data" unless /[A-Za-z0-9]+/ =~ data
> 
> is a good example of data validation ☹

&nbsp;&nbsp;I agree that it isn't... However, most of my uses of regexps are to  
extract some pieces of data from a bigger text - such as

`identify biniou.jpg` =~ /(\d+)x(\d+)/

&nbsp;&nbsp;It would be a pain to have to unanchor those... Moreover, it is way  
better (in my opinion), to write something like

&nbsp;&nbsp;raise "Invalid data" if /[^A-Za-z0-9]/ =~ data

&nbsp;&nbsp;if you really require the full data to be alnum. Or, rather, if you  
want to be somehow more flexible (stripping whitespace and other):

raise "Invalid data" unless /([A-Za-z0-9]+)/ =~ data

data = $1 # Cleaning up data...

&nbsp;&nbsp;Cheers,

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Vincent

> **···**
>
> > On Sat, Mar 24, 2007 at 03:39:42AM +0900, Rick DeNatale wrote:
> > 
> > > On 3/22/07, Robert Klemme \<shortcutter@googlemail.com\> wrote:
> 
> --  
> Vincent Fourmond, PhD student (not for long anymore)  
> [http://vincent.fourmond.neuf.fr/](http://vincent.fourmond.neuf.fr/)

---

<div class="post-metadata">

### Author: ![Rick\_DeNatale1](https://avatars.discourse-cdn.com/v4/letter/r/bbce88/32.png) [@Rick\_DeNatale1](https://rubytalk.org/u/Rick_DeNatale1)
#### Post date: [23 March 2007 21:29 UTC](https://rubytalk.org/t/knocking-lines-out-of-a-multiline-string/36361/16 "2007-03-23T21:29:56Z")

</div>

Well, the OP said "I would like to remove lines matching  
/usr/local/lib from the multiline string:", no mention of at the start  
of a line.

I did actually consider pointing out that he might have meant at the  
beginning of each line, but he didn't so I didn't.

> **···**
>
> On 3/23/07, Brian Candler \<B.Candler@pobox.com\> wrote:
> 
> > On Sat, Mar 24, 2007 at 03:39:42AM +0900, Rick DeNatale wrote:  
> > \> On 3/22/07, Robert Klemme \<shortcutter@googlemail.com\> wrote:  
> > \>  
> > \> \>Bullshit: just use select:  
> > \> \>  
> > \> \> \>\> "foo\nbar\n".select {|l| /^f/ =~ l}  
> > \> \>=\> ["foo\n"]  
> > \>  
> > \> Or, since he's really trying to exclude lines which include /usr/local/lib:  
> > \>  
> > \> str.reject {|s| Regexp.new("/usr/local/lib").match(s)}
> > 
> > Why does nobody seem to anchor regexps?
> 
> --  
> Rick DeNatale
> 
> My blog on Ruby  
> [http://talklikeaduck.denhaven2.com/](http://talklikeaduck.denhaven2.com/)

---

<div class="post-metadata">

### Author: ![Rick\_DeNatale1](https://avatars.discourse-cdn.com/v4/letter/r/bbce88/32.png) [@Rick\_DeNatale1](https://rubytalk.org/u/Rick_DeNatale1)
#### Post date: [27 March 2007 16:12 UTC](https://rubytalk.org/t/knocking-lines-out-of-a-multiline-string/36361/17 "2007-03-27T16:12:02Z")

</div>

Quoting my old friend Kent Beck:

"Make it run before you make it fast."

> **···**
>
> On 3/26/07, Phrogz \<gavin@refinery.com\> wrote:
> 
> > On Mar 23, 12:39 pm, "Rick DeNatale" \<rick.denat...@gmail.com\> wrote:  
> > \> str.reject {|s| Regexp.new("/usr/local/lib").match(s)}
> > 
> > Note that the above results in Regexp#new being called for each line  
> > in the string; not very efficient:
> 
> --  
> Rick DeNatale
> 
> My blog on Ruby  
> [http://talklikeaduck.denhaven2.com/](http://talklikeaduck.denhaven2.com/)

---

<div class="post-metadata">

### Author: ![Andrew\_Stewart](https://avatars.discourse-cdn.com/v4/letter/a/9de053/32.png) [@Andrew\_Stewart](https://rubytalk.org/u/Andrew_Stewart)
#### Post date: [26 March 2007 11:40 UTC](https://rubytalk.org/t/knocking-lines-out-of-a-multiline-string/36361/18 "2007-03-26T11:40:20Z")

</div>

> > \>  
> > \> \>Bullshit: just use select:  
> > \> \>  
> > \> \> \>\> "foo\nbar\n".select {|l| /^f/ =~ l}  
> > \> \>=\> ["foo\n"]  
> > \>  
> > \> Or, since he's really trying to exclude lines which include /usr/local/lib:  
> > \>  
> > \> str.reject {|s| Regexp.new("/usr/local/lib").match(s)}

That's neat. I should have realised that enumberable's methods come into play with a multiline string. No need to mess about with \n characters -- let Ruby handle those.

> > Why does nobody seem to anchor regexps?
> 
> Well, the OP said "I would like to remove lines matching  
> /usr/local/lib from the multiline string:", no mention of at the start  
> of a line.
> 
> I did actually consider pointing out that he might have meant at the  
> beginning of each line, but he didn't so I didn't.

And you were right -- the '/usr/local/lib' sequence isn't at the start of the line. (Though the part between the start of the line and this sequence is predictable and could be worked into the regexp.)

For those interested, the context of the problem was filtering stack traces produced under autotest:

[http://blog.airbladesoftware.com/2007/3/22/filtering-autotest-s-output](http://blog.airbladesoftware.com/2007/3/22/filtering-autotest-s-output)

Thanks for all the help. I greatly appreciate it.

Regards,  
Andy Stewart

> **···**
>
> On 23 Mar 2007, at 21:29, Rick DeNatale wrote:
> 
> > On 3/23/07, Brian Candler \<B.Candler@pobox.com\> wrote:
> > 
> > > On Sat, Mar 24, 2007 at 03:39:42AM +0900, Rick DeNatale wrote:  
> > > \> On 3/22/07, Robert Klemme \<shortcutter@googlemail.com\> wrote:

---

<div class="post-metadata">

### Author: ![Gavin\_Kistner2](https://avatars.discourse-cdn.com/v4/letter/g/b9e5f3/32.png) [@Gavin\_Kistner2](https://rubytalk.org/u/Gavin_Kistner2)
#### Post date: [27 March 2007 23:10 UTC](https://rubytalk.org/t/knocking-lines-out-of-a-multiline-string/36361/19 "2007-03-27T23:10:05Z")

</div>

On the one hand:  
"Premature optimization is the root of all evil"[1]

On the other - knowing that calling a constructor on each iteration of  
a block is much slower when a literal doesn't change - and when it's  
less characters to type as well - might be considered not so much  
premature optimization as...well...reasonable code planning.

[1] [Premature optimization is the root of all evil](http://hans.gerwitz.com/2004/08/12/premature-optimization-is-the-root-of-all-evil.html)

> **···**
>
> On Mar 27, 10:12 am, "Rick DeNatale" \<rick.denat...@gmail.com\> wrote:
> 
> > On 3/26/07, Phrogz \<g...@refinery.com\> wrote:
> > 
> > \> On Mar 23, 12:39 pm, "Rick DeNatale" \<rick.denat...@gmail.com\> wrote:  
> > \> \> str.reject {|s| Regexp.new("/usr/local/lib").match(s)}
> > 
> > \> Note that the above results in Regexp#new being called for each line  
> > \> in the string; not very efficient:
> > 
> > Quoting my old friend Kent Beck:
> > 
> > "Make it run before you make it fast."
