in reply to Regexp match filename, exclude path

Your try, \\.+?\.pdf matches:

  1. A backslash
  2. As little anything as is possible, nongreedy
  3. a dot and then pdf

I think what you would want, instead of matching As little anything as is possible, nongreedy you want Anything except a backslash.

More likely, you just want basename from File::Basename.

Replies are listed 'Best First'.
Re^2: Regexp match filename, exclude path
by cormanaz (Deacon) on Oct 04, 2008 at 17:48 UTC
    Right. I realize why the regexp I tried doesn't work. The solution can't be anything except a backslash, it has to be something like the last set of characters that isn't preceded by a backslash. I realize I could do this with a module, splitting on backslashes then taking the last element, etc., but I want to do it with a regexp if I can.

      I'm not sure what you mean by

      The solution can't be anything except a backslash, it has to be something like the last set of characters that isn't preceded by a backslash.

      The following will match the last sequence of non-backslash characters in your string, which is what you seem to want:

      m![^\\]+\.pdf$!;

      Anchoring the match makes sure you get the filename path, and the character set negation makes sure you get nothing with a backslash in it.