Reputation: 4510
I am trying to remove the Ist part and last part of the file name.
I have a filename like /external/prop/new/test/File.Name.will.BE.this.extension.date
I want to remove the first part of the directory (/external
) and the last part of the filename extension (.date
) so my output file name would be /prop/new/test/File.Name.will.BE.this.extension
eg:
/external/prop/new/test/FACL.Prop.for.BBG.txt.09242012
NEW FILENAME: /prop/new/test/FACL.Prop.for.BBG.txt
OLD FILE name: /external/prop/old/test/set2/FACL.Prop.FITCH.csv.09242012
/prop/old/test/set2/FACL.Prop.FITCH.csv
I had tried something like
my($FinalName, $Finalextension) = split /\.(?!.*\.)/, substr($Fname,$Flength);
but it is not quite helpful.
/external
will always remain the same but the date will always vary and I can't just remove the numbers as the .extension can be numbers.
Upvotes: 0
Views: 924
Reputation: 9019
Try this regex which captures the text you need in $1
:
^\/[^/]+(.*?)\.\d{8}$
This assumes your date is always 8 numbers. You can replace the last \d{8}
with your appropriate date regex.
This can be tested in RegExr here.
Regex Break-up:
^\/
matches the beginning of the line followed by forward slash
(escaped)[^/]+
matches all text until it finds the next forward slash (to
mark the end of /external
)(.*?)
matches AND captures non-greedily all text you need until it finds the last of the pattern\.\d{8}$
matches the last period followed by 8 digit date followed by end of lineUpvotes: 1
Reputation: 4800
$Fname =~ s|^/external||; # Remove leading "external" from path
$Fname =~ s|\.\d{8}$||; # Remove date from end of filename
Upvotes: 2
Reputation: 385565
my $qfn = '/external/prop/new/test/FACL.Prop.for.BBG.txt.09242012';
$qfn =~ s{\.[^.]*\z}{}; # Remove last "extension".
$qfn =~ s{^/[^/]+}{}; # Remove first part of path.
Upvotes: 1