in reply to Get last occurence of a character in a string

Please disregard. I misread the OPs question. This code does the opposite.
my $file = 'c:\folder1\filename.doc'; $file =~ s{ # Substitue... \\ # a backslash [^\\]* # zero or more not-backslashes $ # at the end of string } {}x; # with nothing # allow comments & spaces in search-pattern print $file; # c:\folder1


Unless I state otherwise, all my code runs with strict and warnings

Replies are listed 'Best First'.
Re^2: Get last occurence of a character in a string
by pjotrik (Friar) on Aug 06, 2008 at 13:16 UTC
    You're giving the opposite of what was asked for, and you should escape your backslashes (in the assignment)
    my $path = 'c:\\folder1\\filename.doc'; my ($file) = ($path =~ /([^\\]*)$/);
    (And yes, File::Basename is the way to go in this case)

    UPDATE: Corrected /([^//]*)$/ to /([^\\]*)$/, thx FunkyMonk

      You're giving the opposite of what was asked for
      Yes, you're right. I misread the OP's question.

      and you should escape your backslashes
      Sure?
      my ($file) = ($path =~ /([^//]*)$/);

      Unmatched [ in regex; marked by <-- HERE in m/([ <-- HERE ^/