On Oct 19, Bryan Harris said:

>Does anyone happen to know why this doesn't work as expected?
>
>perl -e '$_="1\n";s/\Z/2/g;print'
>
>Why does it print "2" twice?

It works as *I* expect it to.  \Z matches at the end of the string, OR
before a NEWLINE at the end of the string.  Therefore, in the string
"japhy", \Z matches after the 'y', and in the string "japhy\n", \z matches
after the 'y' and after the '\n'.

Perhaps you want \z, which only matches the end of the string.  If you
want to match at the end of the string, and ignore a final \n, then I
suggest you do:

  s/\Z(?<!\n)/2/g;

That will match \Z, but not if it is AFTER a newline.

-- 
Jeff "japhy" Pinyan         %  How can we ever be the sold short or
RPI Acacia Brother #734     %  the cheated, we who for every service
http://japhy.perlmonk.org/  %  have long ago been overpaid?
http://www.perlmonks.org/   %    -- Meister Eckhart


-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
<http://learn.perl.org/> <http://learn.perl.org/first-response>


Reply via email to