Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > Regular Expression Help - Weird Results

Reply
Thread Tools

Regular Expression Help - Weird Results

 
 
AMT2K5
Guest
Posts: n/a
 
      12-07-2005
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

 
Reply With Quote
 
 
 
 
AMT2K5
Guest
Posts: n/a
 
      12-07-2005
The return is there for now to test one file name.

I am displaying it elsewhere

 
Reply With Quote
 
 
 
 
Matt Garrish
Guest
Posts: n/a
 
      12-07-2005

"AMT2K5" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed) ups.com...
> 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


 
Reply With Quote
 
it_says_BALLS_on_your_forehead
Guest
Posts: n/a
 
      12-07-2005

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/;

 
Reply With Quote
 
it_says_BALLS_on_your_forehead
Guest
Posts: n/a
 
      12-07-2005

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";

 
Reply With Quote
 
it_says_BALLS_on_your_forehead
Guest
Posts: n/a
 
      12-07-2005

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/;

 
Reply With Quote
 
Eric J. Roode
Guest
Posts: n/a
 
      12-07-2005
"AMT2K5" <(E-Mail Removed)> 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
`$=`;$_=\%!;($_)=/(.)/;$==++$|;($.,$/,$,,$\,$",$;,$^,$#,$~,$*,$:,@%)=(
$!=~/(.)(.).(.)(.)(.)(.)..(.)(.)(.)..(.)......(.)/,$"),$=++;$.++;$.++;
$_++;$_++;($_,$\,$,)=($~.$"."$;$/$%[$?]$_$\$,$:$%[$?]",$"&$~,$#,);$,++
;$,++;$^|=$";`$_$\$,$/$:$;$~$*$%[$?]$.$~$*${#}$%[$?]$;$\$"$^$~$*.>&$=`
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Seek xpath expression where an attribute name is a regular expression GIMME XML 3 12-29-2008 03:11 PM
C/C++ language proposal: Change the 'case expression' from "integral constant-expression" to "integral expression" Adem C++ 42 11-04-2008 12:39 PM
Matching abitrary expression in a regular expression =?iso-8859-1?B?bW9vcJk=?= Java 8 12-02-2005 12:51 AM
Getting a list of results from one regular expression tietyt@gmail.com Ruby 6 06-23-2005 07:15 AM
Dynamically changing the regular expression of Regular Expression validator VSK ASP .Net 2 08-24-2003 02:47 PM



Advertisments