From: schuerig@... (Michael Schuerig) Date: 2001-02-07T03:10:02+09:00 Subject: [ruby-talk:10441] Re: Structured text matching? Dave Thomas wrote: > str = File.open("x.html") {|f| f.read} > str =~ /.../m > > > This very much goes against my sense of aesthetics. There's no need > > to read in the file beyond a successful match, and there's no need > > to read further when an orphaned or a tag are > > encountered. > > All true, but at the same time, if you can do it in two lines rather > than writing a full parser, isn't there some compensating gain to be > had? The best solution would be to have someone else write the parser... ;-) > def findTitle(file) > str = '' > loop do > begin > str << file.sysread(2048) > puts "next" > rescue EOFError > raise " not found in file" > end > break if str =~ %{} > end > > return $1 if str =~ %r{.*?(.*?).*?}m > > raise "Couldn't find title in file" > end > > title = findTitle(File.open("test.html")) > puts title > > Can't say as I've tested this, but it _might_ work ;-) I'll see if I use this snippet then. Thanks! Michael -- Michael Schuerig mailto:schuerig@acm.org http://www.schuerig.de/michael/