Regular Expression Help - Weird Results

A

AMT2K5

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
 
M

Matt Garrish

AMT2K5 said:
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
 
I

it_says_BALLS_on_your_forehead

AMT2K5 said:
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/;
 
I

it_says_BALLS_on_your_forehead

it_says_BALLS_on_your_forehead said:
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";
 
I

it_says_BALLS_on_your_forehead

it_says_BALLS_on_your_forehead said:
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/;
 
E

Eric J. Roode

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
`$=`;$_=\%!;($_)=/(.)/;$==++$|;($.,$/,$,,$\,$",$;,$^,$#,$~,$*,$:,@%)=(
$!=~/(.)(.).(.)(.)(.)(.)..(.)(.)(.)..(.)......(.)/,$"),$=++;$.++;$.++;
$_++;$_++;($_,$\,$,)=($~.$"."$;$/$%[$?]$_$\$,$:$%[$?]",$"&$~,$#,);$,++
;$,++;$^|=$";`$_$\$,$/$:$;$~$*$%[$?]$.$~$*${#}$%[$?]$;$\$"$^$~$*.>&$=`
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,754
Messages
2,569,521
Members
44,995
Latest member
PinupduzSap

Latest Threads

Top