Regx to remove all characters after a match

Discussion in 'Perl Misc' started by Duke of Hazard, Apr 18, 2008.

  1. 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!
     
    Duke of Hazard, Apr 18, 2008
    #1
    1. Advertising

  2. 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
     
    John W. Krahn, Apr 18, 2008
    #2
    1. Advertising

  3. 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
     
    Gunnar Hjalmarsson, Apr 18, 2008
    #3
  4. Abigail wrote:
    > Gunnar Hjalmarsson () wrote on VCCCXLIV September
    > MCMXCIII in <URL:news:>:
    > ~~ 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
    <?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
     
    Gunnar Hjalmarsson, Apr 18, 2008
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. dongdong
    Replies:
    2
    Views:
    302
    Paul McGuire
    Mar 25, 2006
  2. werwer

    regx expression? (link included)

    werwer, Feb 2, 2008, in forum: Perl Misc
    Replies:
    2
    Views:
    113
    Brian McCauley
    Feb 2, 2008
  3. Jake Barnes

    regx for newlines -- var regX = /\\n/g;

    Jake Barnes, Feb 19, 2006, in forum: Javascript
    Replies:
    1
    Views:
    94
    Janwillem Borleffs
    Feb 19, 2006
  4. oldyork90

    regx, global efficiencies?

    oldyork90, Jun 9, 2008, in forum: Javascript
    Replies:
    4
    Views:
    104
    oldyork90
    Jun 10, 2008
  5. Cain
    Replies:
    3
    Views:
    105
Loading...

Share This Page