• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 196
  • Last Modified:

Regexp help with capturing (round 3)

Once again, I am stuck on this!  I have asked a similar question before but now I'm trying to do something a little different.  I just can't seem to hack this to where I'm confident with it.  What I'm trying to do is this:

$an1 = 'Album Name';
$an2 = 'The Album Name';
$an3 = 'Album Name (Disc 1)';
$an4 = 'The Album Name (Disc 1)';
$an5 = 'Album Name: Get Some';
$an6 = 'The Album Name: Get Some';
$an7 = '(2004) The Album Name (Disc 1)';
$an8 = '(2004) The Album Name (Disc 1).ext'
$an9 = '(Album Name) Get Some';

for ( all $an's )
{
      if ( $type eq 'FileName' ) {
           s/^(\(\d+\)\s+)?(?:(The|A)\s+)([^(:]+)((?:(\s+\(.*)?)|(?:(\:\s+.*)?))(\..*)?$/$1$3, $2$4$5/i;
        } else {
           # The reverse
           s/^(.*),\s+(The|A)(.*)$/$2 $1$3/i;
        }
}

desired results:

an1 = Album Name
an2 = Album Name, The
an3 = Album Name (Disc 1)
an4 = Album Name, The (Disc 1)
an5 = Album Name: Get Some
an6 = Album Name, The: Get Some
an7 = (2004) Album Name, The (Disc 1)
an8 = (2004) Album Name, The (Disc 1).ext
an9 = (Album Name) Get Some

Basically, I want the word 'The' or 'A' to be appended to the end of the line but before any ':' or '('.  I also want any optional '(2004)\s+' entries at the front of the line to remain (and possibly the file extension).  My regexp above does work .. but I get 'use of uninitialized value' warnings when using 'use warnings'.  I'm also not sure I'm gonna catch all the possibilities with this.  Is there a more efficient / safer way to do this?  I also need to reverse the change .. which does seem to work with the second regexp above.  Thanks once again!
0
Verbatim
Asked:
Verbatim
  • 5
1 Solution
 
VerbatimAuthor Commented:
Well, this also works .. but seems too complicated:

s/^(\(\d+\)\s+)?(?:(The|A)\s+)([^(:]+)((?:(?:\s+\(.*)?)(?:(?:\:\s+.*)?))$/${\($1?$1:'')}$3, $2${\($4?$4:'')}/i;

Also, I can't figure out how to capture the options '.ext' ..

thanks!
0
 
rugdogCommented:
s/(.*)(The|A)\s+([\w| ]+)\b(\W)/\1\3, \2 \4/i
0
 
ozoCommented:
s/(^|(?<=\))\s*)(The|A)\s+([^(:]*\b)/$1$3, $2/;


0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
VerbatimAuthor Commented:
ozo - that definitely works!  Is there a way to optionally grab the '.ext' off the end into $4 or something?  Also, what would be the reverse of this?  Thanks!
0
 
VerbatimAuthor Commented:
So .. I think the reverse would be this:

s/(^|(?<=\)\s))([^(:]*\b),\s(The|A)/$3 $2$1/;

It seems to work, does that look ok?
0
 
VerbatimAuthor Commented:
better yet (for the reverse)?

s/(^|(?<=\)\s))([^(:]*\b),\s(The|A)/$1$3 $2/;
0
 
VerbatimAuthor Commented:
Thanks ozo .. can you have a look at this question also?

http://www.experts-exchange.com/Programming/Programming_Languages/Perl/Q_21119221.html
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

  • 5
Tackle projects and never again get stuck behind a technical roadblock.
Join Now