From: "David A. Black" Date: 2004-03-16T20:32:09+09:00 Subject: Re: reg multi match replace 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 -- David A. Black dblack@wobblini.net