Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > Regx to remove all characters after a match

Reply
Thread Tools

Regx to remove all characters after a match

 
 
Duke of Hazard
Guest
Posts: n/a
 
      04-18-2008
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!

 
Reply With Quote
 
 
 
 
John W. Krahn
Guest
Posts: n/a
 
      04-18-2008
Duke of Hazard 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


That is because . matches any character *except* newline. If you want
it to match a newline as well then you have to use the /s option:

$name =~ s/\n.*//s;


John
--
Perl isn't a toolbox, but a small machine shop where you
can special-order certain sorts of tools at low cost and
in short order. -- Larry Wall
 
Reply With Quote
 
 
 
 
Gunnar Hjalmarsson
Guest
Posts: n/a
 
      04-18-2008
Eric Amick wrote:
> On Thu, 17 Apr 2008 20:23:23 -0700 (PDT), Duke of Hazard
> <(E-Mail Removed)> 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?

--
Gunnar Hjalmarsson
Email: http://www.gunnar.cc/cgi-bin/contact.pl
 
Reply With Quote
 
Gunnar Hjalmarsson
Guest
Posts: n/a
 
      04-18-2008
Abigail wrote:
> Gunnar Hjalmarsson ((E-Mail Removed)) wrote on VCCCXLIV September
> MCMXCIII in <URL:news:(E-Mail Removed)>:
> ~~ Eric Amick wrote:
> ~~ > On Thu, 17 Apr 2008 20:23:23 -0700 (PDT), Duke of Hazard
> ~~ > <(E-Mail Removed)> 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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Why does JavaScript Regx behave weirdly? Cain Javascript 3 09-22-2008 09:26 PM
regx, global efficiencies? oldyork90 Javascript 4 06-10-2008 11:43 AM
regx expression? (link included) werwer Perl Misc 2 02-02-2008 09:55 AM
how to format a return value by using re.sub(regx,rep1,str)? dongdong Python 2 03-25-2006 04:42 PM
regx for newlines -- var regX = /\\n/g; Jake Barnes Javascript 1 02-19-2006 09:49 AM



Advertisments