Regx to remove all characters after a match
Regx to remove all characters after a match
am 18.04.2008 05:23:23 von Duke of Hazard
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!
Re: Regx to remove all characters after a match
am 18.04.2008 06:30:16 von someone
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
Re: Regx to remove all characters after a match
am 18.04.2008 06:37:22 von Eric Amick
On Thu, 17 Apr 2008 20:23:23 -0700 (PDT), 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
>
>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.
--
Eric Amick
Columbia, MD
Re: Regx to remove all characters after a match
am 18.04.2008 10:12:43 von Gunnar Hjalmarsson
Eric Amick wrote:
> On Thu, 17 Apr 2008 20:23:23 -0700 (PDT), 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
>>
>> 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
Re: Regx to remove all characters after a match
am 18.04.2008 11:45:28 von Abigail
_
Gunnar Hjalmarsson (noreply@gunnar.cc) wrote on VCCCXLIV September
MCMXCIII in :
~~ Eric Amick wrote:
~~ > On Thu, 17 Apr 2008 20:23:23 -0700 (PDT), 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
~~ >>
~~ >> 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.
Abigail
--
tie $" => A; $, = " "; $\ = "\n"; @a = ("") x 2; print map {"@a"} 1 .. 4;
sub A::TIESCALAR {bless \my $A => A} # Yet Another silly JAPH by Abigail
sub A::FETCH {@q = qw /Just Another Perl Hacker/ unless @q; shift @q}
Re: Regx to remove all characters after a match
am 18.04.2008 13:43:10 von Gunnar Hjalmarsson
Abigail wrote:
> Gunnar Hjalmarsson (noreply@gunnar.cc) wrote on VCCCXLIV September
> MCMXCIII in :
> ~~ Eric Amick wrote:
> ~~ > On Thu, 17 Apr 2008 20:23:23 -0700 (PDT), 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
> ~~ >>
> ~~ >> 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
$ ./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