From: Raul Parolari Date: 2007-11-28T16:25:23+09:00 Subject: Re: False positives in editing data RichardOnRails wrote: > > Thank you for your further support of my obstinacy 鐃緒申 Your help has > guided me to the solution I wanted. Your original one is succinct, > perhaps even elegant in that it decomposes the problem into two sub- > problems which admit of essentially one-line solutions. While I truly > appreciate that approach, I wanted to find a "natural" solution, > which is the one included below. Hi, Richard you mention your 'obstinacy'... and indeed, you have found a way to implement with Regexps your original design (you did not fool me! :-); great ingenuity. However, as much as I am stunned by your progress (your original program at the top of this post and this one seem like Dante going from Inferno to Paradiso), I must be frank. I do not like building arrays to meet some 'maximum treshold', leaving portions of them empty; it just does not make me 'happy' (in the Matz/Ruby sense, do you understand?). Of course, it is just an array of 5 positions, but it does not matter; it is just echologically wrong for me. I however understand your feeling towards my solution (and the contrast you create with your 'natural one'); got it! but then I invite you to explore scan with \G; look at this, that may be doing something more 'natural': # \G 'anchors' start of next search to end of previous one re_prefix = %r/\G (\d+ [.]?) /x input.each { |line| p line.scan(re_prefix).flatten } Output: ["05"] ["1", "0"] ["2", "002", "1"] ["3", "15", "26", "37"] Small (1 line!), fast, precise: a beauty. It is not the complete solution as '\G scan' in Ruby does not allow you to change the regexp without interrupting the job (there are sad workarounds for it), so the job is not complete. The library StringScanner ('strscan') is of interest, as it solves this problem nicely (and is in C, so is fast). Perhaps, examine \G and/or StringScanner, and see if you can find a solution that meets what you are looking for, without (as seen from me) imperfections. Congrats for your progress (in 1 week!) Raul Some people, when confronted with a problem, think: "I know, I'll use regular expressions". Now they have two problems. Jamie Zawinski :-) -- Posted via http://www.ruby-forum.com/.