#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2009
    Posts
    6
    Rep Power
    0

    Match chars at end of filename, but not extension


    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
    -Z.
  2. #2
  3. CSS & JS/DOM Adept
    Devshed Supreme Being (6500+ posts)

    Join Date
    Jul 2004
    Location
    USA (verifiably)
    Posts
    20,127
    Rep Power
    4304
    Try this:
    Code:
    s/(\s?(?:\[\d\d\]|\(\d\d\))(\.\w+)$)/$1/
    Spreading knowledge, one newbie at a time.

    Check out my blog. | Learn CSS. | PHP includes | X/HTML Validator | CSS validator | Common CSS Mistakes | Common JS Mistakes

    Remember people spend most of their time on other people's sites (so don't violate web design conventions).

IMN logo majestic logo threadwatch logo seochat tools logo