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

Hi fellow monks!

I seek wisdom since nothing I've tried works. I need to get the .mp3 filename (Ozzy Osbourne - Back on earth.mp3) from the following line:

01 02 1 | Ozzy Osbourne | + Back On Earth Ozzy Osbourne - Back on earth.mp3 | 5:00 4689 44.1 +00 128
I can't find a really good pattern to search for since the filename can be different each time and can contain any number of whitespaces.The filename can also look like artist - album - year - track.mp3

Really grateful for some suggesions.

Ciryon

Replies are listed 'Best First'.
Re: regex question
by Masem (Monsignor) on Nov 16, 2001 at 19:41 UTC
    I'm assuming that there's two spaces as opposed to one just before artist and track.mp3. If so, this should do it.
    my $file = ( $line =~ /\s\s(.*?\.mp3)\s\|/ );

    -----------------------------------------------------
    Dr. Michael K. Neylon - mneylon-pm@masemware.com || "You've left the lens cap of your mind on again, Pinky" - The Brain
    "I can see my house from here!"
    It's not what you know, but knowing how to find it if you don't know that's important

Re: regex question
by joealba (Hermit) on Nov 16, 2001 at 20:00 UTC
    I can't golf before my morning coffee... But it works.
    my $line = qq(01 02 1 | Ozzy Osbourne | + Back On Earth Ozzy Osbourne - Back on earth.mp3 | 5: +00 4689 44.100 128); my @pieces = split /\s*\|\s*/, $line; my $file = $1 if $pieces[2] =~ /\s\s(.*?\.mp3)/; print "$file\n";
      Once everyone reminded me about escaping pipes ...
      my $file = (split(/\s*\|\s*/,$line))[2];
      But your way is safer. ;)

      jeffa

      L-LL-L--L-LL-L--L-LL-L--
      -R--R-RR-R--R-RR-R--R-RR
      F--F--F--F--F--F--F--F--
      (the triplet paradiddle)
      
Re: regex question
by rchiav (Deacon) on Nov 16, 2001 at 20:09 UTC
    There's a couple ways you could do it. Here's one, but there can't be a '.' or a '|' in the file name.
    $string=~ /\s*([^.|]+)\.mp3/; print "$1\n";
    Hope this helps,
    Rich

    update
    Use:

    $string=~/\s*([^|]+)\.mp3/;
    The period wasn't needed.
Re: regex question
by Washizu (Scribe) on Nov 16, 2001 at 21:39 UTC

    I came up with this one:

    $string =~ /((\S+\s)*-\s.*\.mp3)/i

    Of course, it is based on the premise that there will always be a '-' character surrounded by spaces between the artist and the song title. It also needs more than one space before the artist's name to parse out the extra data. Hope it helps!

    -----------------------------------
    Washizu
    The best offense is a good offense.