On 01.09.2009 05:35, Lance Pollard wrote:
> What is the recommended way to parse a tree of files and replace
> multiline pattern-matches, if you have say 20 regular expressions you're
> looking for.  I understand how to traverse directories, read/write
> files, and use complex regular expressions, but the question is, what's
> the optimal/recommended way to parse files (find/replace "def
> method_name ...(some lines)... end", with some string, for instance)?
> Is it:
> 
> 1) Read file to String, match string against first pattern, read next
> file into String, match with same pattern... once I've gone through all
> the files with the first pattern, start over with the next pattern.

That's the worst you can do.

> 2) Read file to String, match whole string against all 20 patterns, go
> to next file, match against 20 patterns...

Most efficient of the simple approaches.

> 3) Read file to String, match each line by 20 patterns...

How do you want to do that with multiline patterns?  Is there an easy 
way to convert them to several line based patterns?  If not, you can 
forget this option.

> 4) Something with a Tokenizer which I don't yet understand (if so, could
> you shed some light on it for me :) )


Which Tokenizer are you referring to?  Can you give more detail about 
your patterns or the kind of replacement you want to do?  If not (e.g. 
because they must be generic) the simplest and most efficient seems to 
be option 2 unless we are talking about GB file sizes.

Kind regards

	robert

-- 
remember.guy do |as, often| as.you_can - without end
http://blog.rubybestpractices.com/