Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation
 
PerlMonks  

Drop middle part in a substitution

by James LiGate (Initiate)
on Jan 26, 2004 at 17:58 UTC ( [id://324199]=perlquestion: print w/replies, xml ) Need Help??

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

I suspect this is a dead-simple Perl newbie question (and I'm a dead-simple Perl newbie! :o) ), but darned if I can work it out. I need to process filenames in the format filename.randomcharsEXT where EXT is a three-letter extension of any case (e.g., gif, Jpg, TXT...) and randomchars is guaranteed not to contain a period or any "special" characters. For an input of filename.randomcharsEXT the output needs to be filename.EXT... in other words, I need to drop the random characters between the dot and the extension. This would be straightforward to do across multiple statements, but I'm thinking I should be able to do it with a single s/// and some thoughtful RE. Darned if I can noodle out the syntax, though. Can anyone point me to some references or examples? Nothing I've found in my search so far has helped. Thanks!

Replies are listed 'Best First'.
Re: Drop middle part in a substitution
by Roy Johnson (Monsignor) on Jan 26, 2004 at 18:02 UTC
    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
      "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!

      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.
Re: Drop middle part in a substitution
by Abigail-II (Bishop) on Jan 26, 2004 at 18:08 UTC
Re: Drop middle part in a substitution
by borisz (Canon) on Jan 26, 2004 at 18:08 UTC
    s/[^\.]*(?=(?:jpg|gif|txt)$)//;
    Boris
Re: Drop middle part in a substitution
by Not_a_Number (Prior) on Jan 26, 2004 at 18:58 UTC

    TMTOWTDI. Here's a non-regex solution:

    my $name = 'filename.dqhfhqlkGif'; substr $name, (index $name, '.') + 1, -3, ''; print $name;

    In fact if you change index to rindex, it'll work even if there's more than one period in the name.

    dave

    Update: Also works with filenames containing newlines.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others sharing their wisdom with the Monastery: (4)
As of 2024-04-19 13:52 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found