From: Robert Klemme Date: 2009-09-01T16:20:13+09:00 Subject: Re: Best Practice for Multiline Regexps 2009/9/1 Lance Pollard : > Thanks a lot Robert for your help. > >> Which Tokenizer are you referring to?  Can you give more detail about >> your patterns or the kind of replacement you want to do? > > I would like to be able to do code generation in a kind of preprocessor > way for Actionscript, but I don't want to use a preprocessor because it > seems like too much work (especially in actionscript or java), and I'd > have to do pattern matching anyway :). > > For example, I'd like to convert this: > > [Bindable] > public function get myProperty():String { >     return _myProperty; > } > public function set myProperty(value:String):void { >     _myProperty = value; > } > > ... or this: > > [Bindable] public var myProperty:String; > > ... or anything in between (different formatting...), to this: > > [Bindable(event="myPropertyChange")] > public function get myProperty():String { >     return _myProperty; > } > public function set myProperty(value:String):void { >     _myProperty = value; >     dispatchEvent(new Event("myPropertyChange")); > } > > .... That accessor little snippet can be from 1 to 20+ lines, and I'd > like to be able to just add that line in there without having to run > through the file a bunch of times. > > There's a few other examples similar to that that I'd like to do to, but > that's the gist of it. Ah, I see. I assume your files are not big (looks like program code which typically does not come in GB's.) You might be able to do with single line regular expressions but I'd first try String#gsub! because that's typically the most efficient thing to do. Maybe you can even do this without the block form, e.g. # incomplete contents.gsub! %r{ ( \[Bindable)(\] \s* public \s+ function \s+ get \s+) (\w+) # property name $3 \s* \([^)]*\) .... }x, '\\1(event="\\3Change")\\2\\3...' Kind regards robert -- remember.guy do |as, often| as.you_can - without end http://blog.rubybestpractices.com/