in reply to Why doesn't non-greediness work?

$body =~ s/<img(.+?)="Wink">/;)/g;

Why doesn't the wink smiley captured?

Probably because perl is interpreting this as two commands...
$body =~ s/<img(.+?)="Wink">/; )/g;
The one serious conviction that a man should have is that nothing is to be taken too seriously. -- Nicholas Butler

Replies are listed 'Best First'.
Re: Re: Why doesn't non-greediness work?
by tedrek (Pilgrim) on May 13, 2003 at 02:06 UTC
    actually perl is smart enough to avoid that.. in fact you can do something as disturbing as

    $body =~ s;<img(.+?)="Wink">;\;;g;

    which is why they say only perl can parse perl.

      Yes, but notice that you escaped one of those semicolons... In the original, the semicolon that was part of the smiley was not escaped.

      The one serious conviction that a man should have is that nothing is to be taken too seriously. -- Nicholas Butler
        Please familiarize yourself with the quoting rules in Perl. All of the following work as well:
        $body =~ ss<img(.+?)="Wink">s;)sg; $body =~ s$<img(.+?)="Wink">$;)$g; $body =~ s,<img(.+?)="Wink">,;),g; $body =~ s[<img(.+?)="Wink">]\;)\g; $body =~ s}<img(.+?)="Wink">}{;)}g; $body =~ s&<img(.+?)="Wink">&;)&g; # .. ad nauseam

        Makeshifts last the longest.

Re^2: Why doesn't non-greediness work? (not two commands)
by Aristotle (Chancellor) on May 13, 2003 at 15:27 UTC
    No it isn't. )/g; is not valid Perl so you'd get an error message about that in that case. perl is very smart about inferring the correct delimiter in the overwhelming majority of cases anyway; please check your assumptions next time.

    Makeshifts last the longest.