in reply to Drop middle part in a substitution

s/^(.*\.).*(...)$/$1$2/
Update:
No need to fiddle with capturing and replacing. Just strip out the stuff we don't want:
s/[^.]+(?=...$)//

The PerlMonk tr/// Advocate

Replies are listed 'Best First'.
Re: Re: Drop middle part in a substitution
by James LiGate (Initiate) on Jan 26, 2004 at 18:15 UTC
    "Most magic tricks are easy, once YOU know the secret." -- Marshall Brodien

    Gah! In studying your substitution, it's immediately obvious how it works... but crafting it from scratch was simply beyond me. I guess I just don't "have my head around Perl" yet... but I'll get there eventually, I hope.

    A code snippet that I can just plug in is way more than I was expecting; thank you!

Re: Re: Drop middle part in a substitution
by !1 (Hermit) on Jan 26, 2004 at 18:16 UTC

    Not to be picky but you are aware that newlines are valid in filenames under some OS's, right?

    s/^(\.*\.).*(...)$/$1$2/s;

    Of course, two periods within a filename may also throw this off.

    Update: Ok. You all got me. I'm a dipshit. Thanks.

      s/^(\.*\.).*(...)$/$1$2/s;

      This doesn't work for me:

      $_ = 'filename.12345Gif'; s/^(\.*\.).*(...)$/$1$2/s; print;

      Output:

      filename.12345Gif

      Drop that first backslash :)

      s/^(.*\.).*(...)$/$1$2/s;

      dave

      Two periods should not throw it off: the characters to be discarded were guaranteed not to include any periods, so the greedy match is appropriate.

      As for newlines in filenames: I definitely didn't think of that.


      The PerlMonk tr/// Advocate

      AFAIK msdos fat is the only filesystem with 8.3 filenames. But fat does not allow a newline (in fact it allows only very few characters, and definitly no control characters but \x7f.)

      Not to be picky but you are aware that newlines are valid in filenames under some OS's, right? :)

      If the filename ends with a newline, you'll end up keeping the last 4 characters instead of the last three. Try:

      s/^(.*\.).*(...)\z/$1$2/s;
      instead.