From: Tom Werner Date: 2006-07-13T04:20:20+09:00 Subject: Re: Help with a regexp Daniel Schierbeck wrote: > studlee2@gmail.com wrote: >> Daniel, >> When you grab the data it will be in a string format, so you need >> to convert it to a number (most likely integer). Then you can compare >> it with the size of the second value you grabbed. I would write it >> like this (modify as needed): >> >> print "enter data" >> a = gets >> valid = /^(\d*):(\w*)/ >> >> check = valid.match(a) >> >> if(check[1].to_i == check[2].size) >> >> >> >> end > > Thank you for your reply. > > My problem is that I have a string like this: "3:foo6:monkey5:sheep", > which I need to separate into ["foo", "monkey", "sheep"]. The values > can contain numeric values, so splitting at \d won't work. This is > what makes it difficult: > > "3:ab23:cat5:sheep" => ["ab2", "cat", "sheep"] > > I need to grab the number, then read that many characters, then read > the next number, etc. > > > Cheers, > Daniel > > require 'strscan' array = [] ss = StringScanner.new('3:ab23:cat5:sheep') while !ss.eos? len = ss.scan(/\d+:/).chop.to_i array << ss.peek(len) ss.pos += len end p array => ["ab2", "cat", "sheep"] Tom -- Tom Werner Helmets to Hardhats Software Developer tom@helmetstohardhats.org www.helmetstohardhats.org