Howto print postmatch variable with perl one-liner?

Discussion in 'Perl Misc' started by cibalo, Apr 14, 2012.

  1. cibalo

    cibalo Guest

    Hello,

    I can print the postmatch variable with a perl script as follows:
    #!/usr/bin/perl -w
    $str = "abcdefghi"; $str =~ /def/; print "$`:$&:$'\n";

    For perl one-liner, I can print without the $' as follows:
    $ perl -e '$str = "abcdefghi"; $str =~ /def/; print "$`:$&:\n";'
    abc:def:
    But, I cannot print with $' as follows:
    $ perl -e '$str = "abcdefghi"; $str =~ /def/; print "$`:$&:$'\n";'

    Can you please let me know what I'm missing?
    Thank you very much in advance!!!

    Best Regards,
    cibalo
     
    cibalo, Apr 14, 2012
    #1
    1. Advertising

  2. cibalo

    cibalo Guest

    On Apr 14, 6:59 pm, Ben Morrow <> wrote:
    > After that you're left with various forms of quoting. The cleanest is to
    > avoid $' altogether:
    >
    >     perl -e'"abcdefghi" =~ /(.*?)def(.*)/; print "$1:$2:$3\n"'
    >
    > (obviously with /s if you need it).

    Hello Ben,

    Thank you very much for replying to my post. I do try all of your
    great suggestions. And I note that there is a small typo in your
    second suggestion:
    $ perl -we'"abcdefghi" =~ /(.*?)def(.*)/; print "$1:$2:$3\n"'
    Use of uninitialized value $3 in concatenation (.) or string at -e
    line 1.
    abc:ghi:
    $ perl -we'"abcdefghi" =~ /(.*?)(def)(.*)/; print "$1:$2:$3\n"'
    abc:def:ghi ^ ^
    However, they are and they work like a charm.

    I wish to thank you again for your valuable reply.

    Best Regards,
    cibalo
     
    cibalo, Apr 15, 2012
    #2
    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. Xah Lee
    Replies:
    0
    Views:
    358
    Xah Lee
    Oct 20, 2005
  2. Summercool
    Replies:
    4
    Views:
    92
    Xavier Noria
    Sep 11, 2007
  3. Mark
    Replies:
    8
    Views:
    107
    Tad McClellan
    Apr 29, 2004
  4. Robert Miller

    Deciphering a very cryptic perl "one-liner"

    Robert Miller, Jun 4, 2004, in forum: Perl Misc
    Replies:
    1
    Views:
    366
    Ben Morrow
    Jun 4, 2004
  5. Larry
    Replies:
    1
    Views:
    115
    Martien Verbruggen
    Feb 3, 2005
Loading...

Share This Page