From: Rob Biedenharn Date: 2007-03-23T00:22:17+09:00 Subject: Re: Knocking Lines Out Of A Multiline String On Mar 22, 2007, at 10:43 AM, Andrew Stewart 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: > > /usr/local/lib/ruby/gems/1.8/gems/actionpack-1.13.1/lib/ > action_controller/test_process.rb:382:in `process' > /usr/local/lib/ruby/gems/1.8/gems/actionpack-1.13.1/lib/ > action_controller/test_process.rb:353:in `post' > test/functional/orders_controller_test.rb:241:in > `test_should_handle_errors_on_edit' > > ...to give: > > 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' /usr/local/lib/ruby/gems/1.8/gems/actionpack-1.13.1/lib/ action_controller/test_process.rb:353:in `post' 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 Rob@AgileConsultingLLC.com