From: Paul Vudmaska Date: 2004-03-17T01:24:26+09:00 Subject: Re: reg multi match replace David A. Black wrote: >Hi -- > >On Tue, 16 Mar 2004, Simon Strandgaard wrote: > > > >>On Tue, 16 Mar 2004 12:18:43 +0900, Paul Vudmaska wrote: >> >> >>>i'm still a noob and had the need for a simple template system and thot >>>it would be a nice learning experience. all i want to do is replace >>>{{VALUES}} >>>with the hash value of the same name eg hValues[VALUES]. So i thot well >>>this probably aint easy and i sat down and came up with this >>> >>> @value.gsub!(/(\{\{(.+)\}\})/) { >>> >>> @values[$2] >>> } >>> >>> >>> >>A few hints. >> >> >>".+" is greedy. You would want to use non-greedy ".+?" >> >> >>"." is matching anything else than newline, which easily may match too >>much. Instead use "\w" which only matches alphanumeric characters. >> >> > >Unless your hash keys have non-alphanumeric characters :-) I don't >think there's any danger of matching too much, since it will stop at >the first "}" of a pair. > >Another small suggestion: it doesn't look like you need two captures (unless >you're doing something with $1 that isn't revealed here): > > @value.gsub!(/(\{\{(.+?)\}\})/) { @values[$1] } > > ^^^ > (or \w+? if wanted) > > >David > > > Thanks David, Soon after i fired the email i noticed it did not work on lines with 2 tokens {{one}} {{two}} so adding the ? worked great! About the inner capture, the outer one matched {{match}} and i needed just the name - match - which matches the hash i pass to the template {'match'=>'match value'}. Using @values[$2] matched the inner one. To be honest, i did not know what $2 was, when i was considering my options(like replacing {}), but then i thot "there's a $1 there must be a $2" - and it worked after i put the inner capture. i thot it was great -and a testament to the goodness of all this! The names that will be in the tokens and hashes will be like 'name', 'client.name'. So per your suggestion i changed it to /(\{\{([\w\.]+?)\}\})/ which accomodates the dot and is more concise to boot. Appreciate your help :0) :Paul