Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

I only want to print "$(ROOT)/../../../api/maX \\" in the below code,how can I do that?

#!/usr/bin/perl -w use strict; use warnings; my $file; $file = "./max/base/file.c: $(ROOT)/../../../api/maX \\"; $file =~ /:(.*)/; print "$file\n";

Replies are listed 'Best First'.
Re: printing just part of data
by GrandFather (Saint) on Mar 24, 2011 at 00:24 UTC

    If the part you want to keep starts at a fixed column then use substr to retrieve it:

    #!/usr/bin/perl -w use strict; use warnings; my $file = "./max/base/file.c: \$(ROOT)/../../../api/maX \\"; $file = substr $file, 25; print "$file\n";

    Prints:

    $(ROOT)/../../../api/maX \

    Note in passing that you need to quote the $ for $(ROOT) in double quotes because Perl will see it as the special variable $( otherwise. If you actually wanted two \ characters at the end of the string you need to provide four ('\\\\') because \\ 'escapes' a \ and generates a single \ as a result.

    True laziness is hard work
Re: printing just part of data
by wind (Priest) on Mar 23, 2011 at 23:55 UTC

    Just use \s* to eat up all the spaces, and $1 to access the captured result.

    my $file = './max/base/file.c: $(ROOT)/../../../api/maX \\'; if ($file =~ /:\s*(.*)/) { print "$1\n"; }
Re: printing just part of data
by Anonymous Monk on Mar 24, 2011 at 00:01 UTC
    #!/usr/bin/perl -- use strict; use warnings; my $file = "./max/base/file.c: $(ROOT)/../../../api/maX \\"; my ( $left, $right ) = split /:\s{4,}/, $file ; use Data::Dumper; print Dumper( $file, $left, $right ); __END__ $VAR1 = './max/base/file.c: 0ROOT)/../../../api/maX \\'; $VAR2 = './max/base/file.c'; $VAR3 = '0ROOT)/../../../api/maX \\';
    I hope you noticed that "" interpolate :)