Howto print postmatch variable with perl one-liner?

C

cibalo

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
 
C

cibalo

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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,013
Latest member
KatriceSwa

Latest Threads

Top