From: Robert Klemme Date: 2007-05-02T22:25:04+09:00 Subject: Re: Inverse scanf: finding format specifers of existing fields On 02.05.2007 15:08, Bil Kleb wrote: > Robert Klemme wrote: >> >> If there is a fixed number of formats you can probably use a cascade >> of RX matches. > > Unfortunately not. > >> Otherwise it probably becomes a bit more complex like matching >> sequences of digits and measuring their lengths. >> >> >> md = %r{^(\d+)\.(\d+)?$}.match('0.4577') >> => # >> >> pa="%#{md[0].size}.#{md[2].size}f" > > Hmmm, this looks like a viable path. > > I hadn't thought of using MatchData groups, but as you say, > it may get ugly fast... I'm thinking of edge cases like > dealing with the leading space if positive numbers become > negative, or accommodating the number of digits needed for > exponentials or integers if the new number exceeds the > capacity of the existing format. For floating point numbers you might even get away with a single regexp if that is crafted appropriately and group values are evaluated accordingly. Kind regards robert