Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"
 
PerlMonks  

Why can't I use s///

by njack (Novice)
on May 08, 2013 at 16:45 UTC ( [id://1032637]=perlquestion: print w/replies, xml ) Need Help??

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

Hi everyone, I've got a problem I just cant figure out... I want to remove the initial part of some strings. Example:

"../files/jack/2012/derpone.pdf"

should become "derpone.pdf"

So i have this:

my $text="../files/jack/2012/derpone.pdf"; $text=~ s/..\/\w*\/\w*\/\d\d\d\d\///g;

And it works like a charm on Padre. But when I load the .cgi file on the server of my university it doesn't work anymore.
my $text="../files/jack/2012/derpone.pdf"; $text=~ s/..\/\w*\/\w*\/\d\d\d\d\///g; $cgi->h1($text),
It' a copy paste and yet on my webpage i get ../files/jack/2012/derpone.pdf

the regular expression should: ../letters/letters/4 numbers/

What is the problem?

Replies are listed 'Best First'.
Re: Why can't I use s///
by toolic (Bishop) on May 08, 2013 at 16:48 UTC
    If your sting is always a file path, you could try File::Basename:
    use warnings; use strict; use File::Basename qw(basename); my $text = "../files/jack/2012/derpone.pdf"; $text = basename($text); print "$text\n"; __END__ derpone.pdf

    Note that .. in your regex means any character, not just dots.

      Yeah about file::basename my hands are tied since I can't install libraries on the university server, so I can't use it.

      And about the .. it's fine since all my strings begins witn ".." anyway

        It should be installed already because it is a Core module. Give it a try.

        If it doesn't work, use Tip #3 from the Basic debugging checklist

Re: Why can't I use s///
by Random_Walk (Prior) on May 08, 2013 at 17:00 UTC

    Will it work if you just grab the last characters that are not a slash?

    # something like my $text="../files/jack/2012/derpone.pdf"; $text=~ s|.*/([^/]*$)|$1|g;

    Cheers,
    R.

    Pereant, qui ante nos nostra dixerunt!
      Even simpler:
      s!.*/!!;
      Just another Perler interested in Algol Programming.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://1032637]
Approved by toolic
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others lurking in the Monastery: (8)
As of 2024-04-23 15:38 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found