in reply to REGEX: muliple search and replace

As one Regexp, I would definetly go with:

s/<!---var(\d+)--->/$var{$1}/g;
This, of course, assinging $var{"1"} or whatever with the HTML you want. Sort of a poor-man's template, eh?

--
$Stalag99{"URL"}="http://stalag99.keenspace.com";

Replies are listed 'Best First'.
RE: Re: REGEX: muliple search and replace
by dws (Chancellor) on Nov 15, 2000 at 01:34 UTC
    I use a variation on this that allows for both recursive expansion and late binding.
    $vars{foo} = sub { int(rand(1) ? "bar" : "baz" };
    $vars{bar} = "vi";
    $vars{baz} = "emacs";
    
    print expand_template("<!--foo--> rules!");
    
    sub expand_template {
        my $template = shift;
        $template =~ s/<!--(\w+)-->/expand_template(expand_var($1))/ge;
        return $template;
    }
    
    sub expand_var {
        my $name = shift;
        return "" if not exists $vars{$name};
        return &{$vars{$name}}() if ref $vars{$name} eq "CODE";
        return $vars{$name};
    }