From: "David A. Black" Date: 2004-08-17T00:48:05+09:00 Subject: Re: Rubish Way of extracting elements Hi -- On Tue, 17 Aug 2004, Daniel V�lkerts wrote: > I started written a little script to analyse my syslogs. The development > went on very fast, but today I'm searching the rubish way to dissect a > string into some parts. For example in my syslog there is a line (valid > as described in rfc3146) > > <165> Aug 16 17:01:35 localhost Just a test > > I was trying to reach this form > > var = content > > pri = 165 > timestamp = Aug 16 17:01:35 > device = localhost > msg = Just a test > > But how do I accomplish this? I read the pickaxe book, but the example I > found was about repeating values e.g. | as seperator. Is a suitable > regexp the way or should use another technique e.g. String#index etc.? > > > Thanks for your time helping me, I'll pay it back if I become a little > more rubisher ;) You could match it to a regular expression, and grab the results in ()-expressions: str = "<165> Aug 16 17:01:35 localhost Just a test" pri, timestamp, device, msg = /<(\d+)>\s+(\w+\s+\d+\s+[\d:]+)\s+(\S+)\s+(.*)/.match(str).captures Another way would be to use scanf. This has the advantage that you get your 165 as an integer (if that's important): require 'scanf' pri, timestamp, device, msg = str.scanf("<%\d> %15c %s%*c %[\\S\\s]" (You might have to adjust either the regex or the format string depending on how consistent and predictable the lines are.) David -- David A. Black dblack@wobblini.net