From: gedb@... (Ged) Date: 2004-02-09T07:04:09+09:00 Subject: Re: principle of most suprise Joel VanderWerf wrote in message news:<401ADCE4.2000209@path.berkeley.edu>... > Dave Lee wrote: > > tony summerfelt wrote: > > > >>gah, ruby is doing it to me again: > >> > >> logline=String.new("+ 30 Jan 12:20:09 [3988] addr: x.x.x.x") > >> tda=Array[(logline.split(/\[\d+\]/))] > >> tda.first > > > > > > another way of writing this code, which you may find more explicit if you > > only use tda.first is to use MatchData#pre_match > > > > logline = "+ 30 Jan 12:20:09 [3988] addr: x.x.x.x" > > marker = /\[\d+\]/ > > marker.match(logline).pre_match # equivalent to tda.first from above > > Or using the lookahead contruct, ?=, you can do this: > > logline = "+ 30 Jan 12:20:09 [3988] addr: x.x.x.x" > logline[/^.*(?=\[\d+\])/] Another alternative is: logline=String.new("+ 30 Jan 12:20:09 [3988] addr: x.x.x.x") tda, number,address=(logline.split(/\[|\]|(?:addr\:)/, 3)) puts "Putting it all back together: #{tda} [#{number}] addr: #{address}" The most surprising thing is that programming can be so easy.