From: Robert Klemme Date: 2005-02-12T21:49:59+09:00 Subject: Re: Need advice on recognizing patterns in strings and replacing them with other patterns... "Christian Neukirchen" schrieb im Newsbeitrag news:m2fz0282jw.fsf@lilith.local... > "Harry Truax" writes: > >> Hi, >> >> I am working on a project which involves reading lines from a file, one >> by >> one, looking for certain patterns in each line, converting the patterns >> into >> new >> ruby code which may use some of the info in the recognized patterns. I >> think >> this would be a good application for Ruby's built in regular expression >> and >> pattern/substitution abilities. I would greatly appreciate any advice on >> a >> nifty way to approach this. I have some examples of what I want to do >> below: >> >> Input from my source file: >> >> box2_34 = $CUR % ((box2_20 + (box2_21) + box2_22) - (box2_25 + box2_27 + >> box2_29 + box2_31 + box2_33)) >> >> Convert the above to: >> >> box2_34 = $CUR % (box2_20.to_f + box2_21.to_f + box2_22.to_f - >> (box2_25.to_f >> + box2_27.to_f + box2_29.to_f + box2_31.to_f + box2_33.to_f)) > > Would it maybe easier to write a Domain-Specific Language and then > simply eval the source file? The input looks to me like syntactically > correct Ruby... Apart from that it does not exactly what he wants it to do. But I have thought of this also: if you make sure that all variables are float values the ".to_f"'s become superfluous and simply evaluating would work (apart from possible security issues). IMHO the proper way to tackle this would be a parser of this domain language and a processor that works on the syntax tree to output the desired results. Kind regards robert