From: Robert Klemme Date: 2010-07-06T00:51:15+09:00 Subject: Re: File data extraction 2010/7/5 Brian Candler : > Rolf Pedersen wrote: >> The code now looks like this: >> >> filenames = File.open(filename).readlines.join.scan(/^@begin >> Objects\n(.*?)^@end;/m)[0][0].split("\n").map{|l| >> l.scan(/"(.*?)"/)[0][0]} >> Probably far from optimal, but it seems to do the trick. > > That's the most important thing :-) > > I actually misread your example. If there's only one @begin Objects > section, then 'scan' is overkill; a simple regexp match will do. > > res = if File.read(filename) =~ /^@begin Objects$(.*?)^@end;$/m >  $1.scan(/^\s*"(.*?)"/).map { |r| r.first } > end If files are large than the line based approach is usually more feasible. In this case you can use the flip flop operator in an if condition to select the lines we want: 17:31:49 Temp$ ./lextr.rb ["n_cst_xml_utils.sru", "n_melding.sru"] 17:48:32 Temp$ cat lextr.rb #!/bin/env ruby19 ar = [] DATA.each_line do |line| if /^@begin Objects/ =~ line .. /^end;/ =~ line name = line[/^\s*"([^"]*)"/, 1] and ar << name end end p ar __END__ Save Format v3.0(19990112) @begin Libraries "felles.pbl" ""; @end; @begin Objects "n_cst_xml_utils.sru" "felles.pbl"; "n_melding.sru" "felles.pbl"; @end; 17:49:30 Temp$ Kind regards robert -- remember.guy do |as, often| as.you_can - without end http://blog.rubybestpractices.com/