From: bbiker Date: 2007-07-26T10:09:59+09:00 Subject: Re: A method to search and cut in Ruby. On Jul 25, 7:05 pm, Phrogz wrote: > On Jul 25, 4:59 pm, scomb...@gmail.com wrote: > > > For example if I have a file called filename.txt and I want to search > > for this ---> "Compression : JPEG (old-style)" > > but only return everything to the right of the delimitator? > > C:\>irb > irb(main):001:0> s = "Foo : Bar" > => "Foo : Bar" > irb(main):002:0> s.split ":" > => ["Foo ", " Bar"] > irb(main):003:0> s.split(":").last > => " Bar" > > ...or did you want the leading whitespace chomped? > > irb(main):004:0> s.split( /\s*:\s*/ ) > => ["Foo", "Bar"] > irb(main):005:0> s.split( /\s*:\s*/ ).last > => "Bar" Some of the text I'm grabbing can contain additional colons and I want everything after the first one. SC I am assuming you want everything after the first colon but not after the second irb(main):001:0> line = "Compression : JPEG (old_style) : whatever" => "Compression : JPEG (old_style) : whatever" irb(main):002:0> if line =~ /^Compression\s*:?(.*)$/ irb(main):003:1> end_str = $1 irb(main):004:1> first_el = end_str.split(/:/).first puts first_el irb(main):005:1> end JPEG (old_style) if you everything after each colon el = end_str.split(/:/) p el = {"JPEG (old-style)", "whatever"]