Regular Expression Help - Weird Results

Discussion in 'Perl Misc' started by AMT2K5, Dec 7, 2005.

  1. AMT2K5

    AMT2K5 Guest

    Hello, I have a function that will so far convert an mp3 filename from
    Track-Artist_moreartist.mp3

    to Artist_moreartist_Track.mp3

    *Where more artist is like Pink_Floyd not Queen*

    I am getting weird results with my regular expression result

    my @dir_contents = @_[0];
    foreach(@dir_contents)
    {
    my $old = $_;
    my $track;
    my $artist;
    my $fullTitle;

    $old =~ m/-/;
    $track = $`;

    my $position = rindex($_ ,"-") + 1;

    $artist = substr($_,$position);
    $artist =~ s/\.mp3//;
    $artist =~ s/^\s+//;
    $artist =~ s/\s+$//;

    $track =~ s/^\s+//;
    $track =~ s/\s+$//;

    $fullTitle = $artist."-".$track.".mp3";

    return $fullTitle;

    }

    Original is Echoes-Derek_Duke.mp3

    My result is (whitespace at the begining still)
    \sDerek_Duke-Echoes.mp3 -.mp3

    Appreciate any help whatsoever.

    -Aaron
    AMT2K5, Dec 7, 2005
    #1
    1. Advertising

  2. AMT2K5

    AMT2K5 Guest

    The return is there for now to test one file name.

    I am displaying it elsewhere
    AMT2K5, Dec 7, 2005
    #2
    1. Advertising

  3. AMT2K5

    Matt Garrish Guest

    "AMT2K5" <> wrote in message
    news:...
    > The return is there for now to test one file name.
    >


    The return is where?

    > I am displaying it elsewhere
    >


    Well, I'm glad you're not showing it off in public.

    Please put enough context in your posts for people to understand what you're
    talking about.

    Matt
    Matt Garrish, Dec 7, 2005
    #3
  4. AMT2K5 wrote:
    > Hello, I have a function that will so far convert an mp3 filename from
    > Track-Artist_moreartist.mp3
    >
    > to Artist_moreartist_Track.mp3
    >
    > *Where more artist is like Pink_Floyd not Queen*
    >


    try (untested).

    my $song =~ s/(\w+)-(\w+)_(\w+)/$2_$3_$1/;
    it_says_BALLS_on_your_forehead, Dec 7, 2005
    #4
  5. it_says_BALLS_on_your_forehead wrote:
    > AMT2K5 wrote:
    > > Hello, I have a function that will so far convert an mp3 filename from
    > > Track-Artist_moreartist.mp3
    > >
    > > to Artist_moreartist_Track.mp3
    > >
    > > *Where more artist is like Pink_Floyd not Queen*
    > >

    >
    > try (untested).
    >
    > my $song =~ s/(\w+)-(\w+)_(\w+)/$2_$3_$1/;


    make that:


    my $song = 'Echoes-Derrick_Pink_Floyd.mp3';
    print "$song\n";
    $song =~ s/(\w+)-(\w+)_(\w+)/$2_$3_$1/;
    print "$song\n";
    it_says_BALLS_on_your_forehead, Dec 7, 2005
    #5
  6. it_says_BALLS_on_your_forehead wrote:
    > it_says_BALLS_on_your_forehead wrote:
    > > AMT2K5 wrote:
    > > > Hello, I have a function that will so far convert an mp3 filename from
    > > > Track-Artist_moreartist.mp3
    > > >
    > > > to Artist_moreartist_Track.mp3
    > > >
    > > > *Where more artist is like Pink_Floyd not Queen*
    > > >

    > >
    > > try (untested).
    > >
    > > my $song =~ s/(\w+)-(\w+)_(\w+)/$2_$3_$1/;

    >
    > make that:
    >
    >
    > my $song = 'Echoes-Derrick_Pink_Floyd.mp3';
    > print "$song\n";
    > $song =~ s/(\w+)-(\w+)_(\w+)/$2_$3_$1/;
    > print "$song\n";


    hehe, actually, make that:
    $song =~ s/(\w+)-(\w+)/$2_$1/;
    it_says_BALLS_on_your_forehead, Dec 7, 2005
    #6
  7. "AMT2K5" <> wrote in news:1133918409.805640.38190
    @z14g2000cwz.googlegroups.com:

    > foreach(@dir_contents)
    > {
    > my $old = $_;


    There is a shorter, simpler notation for the above:

    foreach my $old (@dir_contents)


    > $old =~ m/-/;
    > $track = $`;
    >
    > my $position = rindex($_ ,"-") + 1;
    >
    > $artist = substr($_,$position);
    > $artist =~ s/\.mp3//;
    > $artist =~ s/^\s+//;
    > $artist =~ s/\s+$//;
    >
    > $track =~ s/^\s+//;
    > $track =~ s/\s+$//;


    You're not using regular expressions to help you as much as you could.
    All you're using the regexes for is to find a hyphen (why do you use m/-/
    one time, and rindex the other?), and to trim bits off of the pieces you
    do find.

    Try "capturing" in your regex. That means using parentheses around bits
    of your regex; this causes the parenthesized bits to be returned (in list
    context). See:

    ($track, $artist) = $old =~ /(.*?)-(.*)/;

    (ASIDE: you should always (yeah, always) check to see if the regex
    succeeded:

    unless (($track, $artist) = $old =~ ....)
    { ... handle non-match ... }
    END ASIDE)

    With capturing, you can be more specific about your matching:

    ($track,$artist) = $old =~ /\A\s*(.*?)\s*-\s*(.*?)\s*(\.mp3)?\s*\z/;

    Now, that's a pretty incomprehensible bit of line noise, isn't it? But
    that's why they invented the 'x' modifier. It lets you insert arbitrary
    whitespace and comments into regular expressions. Use it. Use it often.

    ($track,$artist) = $old =~
    /
    \A # beginning of string
    \s* # Optional leading whitespace
    (.*?) # track name
    \s* - \s* # separator (with optional whitespace)
    (.*?) # artist name
    \* # optional trailing whitespace
    (\.mp3)? # optional ".mp3" suffix
    \s* # optional trailing whitespace
    \z # end of string
    /x; # end of regex. Note x modifier.


    --
    Eric
    `$=`;$_=\%!;($_)=/(.)/;$==++$|;($.,$/,$,,$\,$",$;,$^,$#,$~,$*,$:,@%)=(
    $!=~/(.)(.).(.)(.)(.)(.)..(.)(.)(.)..(.)......(.)/,$"),$=++;$.++;$.++;
    $_++;$_++;($_,$\,$,)=($~.$"."$;$/$%[$?]$_$\$,$:$%[$?]",$"&$~,$#,);$,++
    ;$,++;$^|=$";`$_$\$,$/$:$;$~$*$%[$?]$.$~$*${#}$%[$?]$;$\$"$^$~$*.>&$=`
    Eric J. Roode, Dec 7, 2005
    #7
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. VSK
    Replies:
    2
    Views:
    2,281
  2. =?iso-8859-1?B?bW9vcJk=?=

    Matching abitrary expression in a regular expression

    =?iso-8859-1?B?bW9vcJk=?=, Dec 1, 2005, in forum: Java
    Replies:
    8
    Views:
    839
    Alan Moore
    Dec 2, 2005
  3. GIMME
    Replies:
    3
    Views:
    11,940
    vforvikash
    Dec 29, 2008
  4. dorayme
    Replies:
    1
    Views:
    610
    richard
    Jan 21, 2011
  5. Replies:
    6
    Views:
    148
Loading...

Share This Page