|
Prev: FAQ 9.25 How do I fetch/put an FTP file?
Next: odd chars in perldoc prior to 5.10.0 (was: Re: How to print question)
From: Gunnar Hjalmarsson on 18 Apr 2008 07:43 Abigail wrote: > Gunnar Hjalmarsson (noreply(a)gunnar.cc) wrote on VCCCXLIV September > MCMXCIII in <URL:news:66r3c2F2h2crbU1(a)mid.individual.net>: > ~~ Eric Amick wrote: > ~~ > On Thu, 17 Apr 2008 20:23:23 -0700 (PDT), Duke of Hazard > ~~ > <squash(a)peoriadesignweb.com> wrote: > ~~ > > ~~ >> I can not figure out why this is not printing just 123: > ~~ >> > ~~ >> $name = "123\n456\n789"; > ~~ >> > ~~ >> $name =~ s/\n.*//; > ~~ >> > ~~ >> print $name; > ~~ >> > ~~ >> which outputs: > ~~ >> > ~~ >> 123 > ~~ >> 789 > ~~ >> > ~~ >> If I write it in php using preg_replace , it works! > ~~ > > ~~ > By default, '.' in Perl regexes does not match newline. If you want it > ~~ > to match newline, use > ~~ > > ~~ > $name =~ s/\n.*//s; > ~~ > > ~~ > I don't know PHP, but it surprises me that it handles that case > ~~ > differently. > ~~ > ~~ A bug in PHP? > > It would do what the OP intended in Perl6 as well. Maybe so, but the PHP docs say: ". match any character except newline (by default)" And still: $ cat test.php #!/usr/bin/php <?php echo preg_replace('/\n.*/', '', "123\n456\n789") ?> $ ./test.php Content-type: text/html X-Powered-By: PHP/4.3.3 123 $ -- Gunnar Hjalmarsson Email: http://www.gunnar.cc/cgi-bin/contact.pl |