From: James Edward Gray II Date: 2005-11-06T22:21:30+09:00 Subject: Re: Help requested -- regexp On Nov 5, 2005, at 11:53 PM, Srinivas Jonnalagadda wrote: > I have a text file with each line representing a 'record'. The line > has tab-separated 'field=value' values. *Note*: Not all fields are > mandatory in all records. Well, that's crying out to be a Hash, right? Hash[*line.split("\t").map { |f| f.split("-") }.flatten] > This text file is rather large, and is auto-dumped from another > application. You may not want to preload it then, if the users don't mind waiting for query() (or whatever) to check it line by line. > Now, I am trying to provide a quick interface to my users to query > this file. The interface I am looking at is something on the lines of > > (field1 > 4) and ((field4 < 2.25) or (field3 > 8.0)) > > to be typed in the query shell. You could use irb for the "shell" and Ruby's block syntax for the query. If you want the fields to work as you show them above, use a little method_missing magic. > I had first written something similar to: > > class Field > > ... > > def gen_regexp(cond) > regexp = "(md = /#{@name}=(.+?)/.match(_line); md and " > regexp += cond.gsub(/#{@name}/, 'md[1].' + @converter_method) > # 'to_i'/'to_f' > regexp += ')' > end > > ... > > end > > This 'generated' regexp would then be substituted in the place of the > corresponding parenthesized 'condition'. Once all such substitutions > are complete, a query block is generated as: > > query_blk = eval("lambda { |_line| #{final_regexp} }") > > This query block is then used in a conventional 'select' on the lines. > > It worked for the likes of the example above, but started having > problems for clauses with multiple fields in each 'condition', like: > > (field1 > 4) and ((field4 < 2.25) or (field3 + field8 > 8.0)) > > since the above substitution logic is at an individual field level. If you want to express complete relationships, just use Ruby code as described above. > Suggestions please. Thanks! Those are my best thoughts. Hope it helps. James Edward Gray II