in reply to s// problem

Try making your quantifier, in this case +, non-greedy, with a ? after it. Look it up on perlre, search for 'greedy'.

Updated:Look up Common Regex Gotchas in our Tutorials secion.

Update 2: As loc and JayBonci mention, it is most likely not your regex, although I would still look into not making it greedy, but your hash might have a problem. Also there are quite a few templating systems out there, for HTML, etc... might be worth looking into, since you might run into lots of exceptions, and regexes, as powerful as they are, might not handle it (cleanly).

Update 3: Again, helped by someone else, /i modifier might provide the wrong key, since hash keys, since they *are* case sensitive.

Replies are listed 'Best First'.
Re: Re: s// problem
by JayBonci (Curate) on Apr 12, 2002 at 04:16 UTC
    But the "greedy" operator wouldn't be a problem in his case (not to say that it is a bad idea). If the greedy operator was a problem
    $str = "%tag1% went to %tag2% #with $str = ~ s/\%(\S+)\%/$template{$1}/gi;

    Would work just fine. However, the greedy operator syndrome would affect a test file such as:
    $str = "%tag1%ALONGSTRINGOFTEXT%tag2% #with $str = ~ s/\%(\S+)\%/$template{$1}/gi; #would yield: #$str eq $template{"tag1%ALONGSTRINGOFTEXT%tag2"};

    ...because the \S+ would match all occurances of muliple-non-whitespace-then-a-percent. In this case, for that test file, it works fine, because there is whitespace demarkating the replacement targets. (\S+?) is a really good idea, but I would also look into the problem in your %template

        --jb
Re: Re: s// problem
by Hofmator (Curate) on Apr 12, 2002 at 11:53 UTC
    Try making your quantifier, in this case +, non-greedy, with a ? after it.

    I don't think this is necessary here. After all the tag is already surrounded by special symbols, the percentage sign ('%'). So why not just use the greedy modifier with a negated character class (of course you can't have any '%' chars in the tag names): s/\%([^%]+)\%/$template{$1}/g;

    -- Hofmator