CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Regular Expressions:
Matching Pattern 4 characters at end of file name, not extension

 



zumbrujm
New User

Oct 15, 2009, 6:31 AM

Post #1 of 4 (3551 views)
Matching Pattern 4 characters at end of file name, not extension Can't Post

Hello,

I have a list of file names like this:
Song title (30).mp3
Another song title [02].MP3
A final song (57).wma

What I would like to do is remove those track numbers. The regex I have so far, while not quite working correctly, will match " (57).wma" and replace it with nothing. Instead, I want to preserve the file extension.


Code
s/(\s?((\[\d\d\]|\(\d\d\))\....)$)//


Is there a way to specify for the $ anchor to start looking x characters from the end, so it won't match the file extension?

Thanks,
John


shawnhcorey
Enthusiast


Oct 15, 2009, 10:04 AM

Post #2 of 4 (3543 views)
Re: [zumbrujm] Matching Pattern 4 characters at end of file name, not extension [In reply to] Can't Post

End it with not a period:

Code
s/(\s?((\[\d\d\]|\(\d\d\))\.[^\.]*)$)//


__END__

I love Perl; it's the only language where you can bless your thingy.

Perl documentation is available at perldoc.perl.org. The list of standard modules and pragmatics is available in perlmodlib.

Get Markup Help. Please note the markup tag of "code".


zumbrujm
New User

Oct 15, 2009, 12:02 PM

Post #3 of 4 (3540 views)
Re: [shawnhcorey] Matching Pattern 4 characters at end of file name, not extension [In reply to] Can't Post

Thanks for the reply, though I don't quite see how that works. It appears to me that it will include the file extension as part of the "found" expression. In other words, for a file name "Happy Days [29].Mp3", " [29].Mp3" will be stored in $1. what I'd prefer is to have " [29]" stored in $1, that way I can just replace it with nothing.


shawnhcorey
Enthusiast


Oct 15, 2009, 12:22 PM

Post #4 of 4 (3538 views)
Re: [zumbrujm] Matching Pattern 4 characters at end of file name, not extension [In reply to] Can't Post

Do you want the track numbers removed from the name or do you want to extract them?

To extract:

Code
#!/usr/bin/env perl 

use strict;
use warnings;

while( <DATA> ){
chomp;
m{ ( (?: \( \d\d \) ) | (?: \[ \d\d \] ) ) (?: \. [^.]* ) \z }msx;
print "$1 $_\n";
}

__DATA__
Song title (30).mp3
Another song title [02].MP3
A final song (57).wma


To remove them:

Code
#!/usr/bin/env perl 

use strict;
use warnings;

while( <DATA> ){
chomp;
s{ \s* (?: (?: \( \d\d \) ) | (?: \[ \d\d \] ) ) (?= \. [^.]* ) }{}msx;
print "$_\n";
}

__DATA__
Song title (30).mp3
Another song title [02].MP3
A final song (57).wma


__END__

I love Perl; it's the only language where you can bless your thingy.

Perl documentation is available at perldoc.perl.org. The list of standard modules and pragmatics is available in perlmodlib.

Get Markup Help. Please note the markup tag of "code".

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives