Reg multi match replace

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]
        }   

…and was simply astonished to see that’s all i needed for the ‘real
work’ of this template- so i just wanted to say,/ man how cool is that/!!

thanks matz, noobie me

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.

@value.gsub!(/({{(\w+?)}})/) do
@values[$2]
end

···

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]
        }   


Simon Strandgaard

Hi –

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 :slight_smile: 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

···

On Tue, 16 Mar 2004, Simon Strandgaard wrote:

On Tue, 16 Mar 2004 12:18:43 +0900, Paul Vudmaska wrote:


David A. Black
dblack@wobblini.net

“Simon Strandgaard” neoneye@adslhome.dk schrieb im Newsbeitrag
news:pan.2004.03.16.10.50.19.188882@adslhome.dk…

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.

While we’re at it: you also don’t need the outer brackets.

@value.gsub!(/({{(\w+?)}})/) do
@values[$2]
end

@value.gsub!(/{{(\w+?)}}/) do
@values[$1]
end

:wink:

robert
···

On Tue, 16 Mar 2004 12:18:43 +0900, Paul Vudmaska wrote:

David A. Black wrote:

Hi –

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 :slight_smile: 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

···

On Tue, 16 Mar 2004, Simon Strandgaard wrote:

On Tue, 16 Mar 2004 12:18:43 +0900, Paul Vudmaska wrote:

Hi –

···

On Wed, 17 Mar 2004, Paul Vudmaska wrote:

David A. Black wrote:

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)

The thing is, you don’t need an inner and outer match in the first
place, if all you want is the inner stuff. You’re just saving an
extra piece of data that you’re not using.

David


David A. Black
dblack@wobblini.net

David A. Black wrote:

Hi –

David A. Black wrote:

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)

The thing is, you don’t need an inner and outer match in the first
place, if all you want is the inner stuff. You’re just saving an
extra piece of data that you’re not using.

David

Right, David, now i see that - pretty cool, thanks! :stuck_out_tongue:

···

On Wed, 17 Mar 2004, Paul Vudmaska wrote: