Swap Last Name, First Name w/ Multi Names and & sign

A swapping-ground for Regular Expression syntax

Swap Last Name, First Name w/ Multi Names and & sign

Postby penguinaka » Sun Jun 12, 2011 12:39 am

Thanks much for answering my last question. Here is another that has me stumped. I'm bulk renaming lots of ebooks. Many of the files have the Last name First like so:

Carlin, George P - Stupid Jokes 1 - Your Mama!.epup

this is what im using:

INFO -----: Swap Lastname, Firstname if in front of a title or series with a dash(-)
EXAMPLE-: Carlin, George P - Stupid Jokes 1 - Your Mama!.epup
RESULT--: George P Carlin - Stupid Jokes 1 - Your Mama!.epub
FIND------: ^(\w+), *([\w \.]+)[ ]+-[ ]*(.*)
REPLACE-: \2 \1 - \3
OR--------: (Depending on what program your using)
REPLACE-: $2 $1 - $3

But it won't work on file names that are like this:

Carlin, George P & Swift, Taylor L. L. - Stupid Jokes 1 - Your Mama!.epup

The intials could have periods. The file should end up looking like this.

George P Carlin & Taylor L. L. Swift - Stupid Jokes 1 - Your Mama!.epup

any ideas? Thanks for your time btw.
penguinaka
 
Posts: 4
Joined: Fri Jun 10, 2011 7:56 am

Re: Swap Last Name, First Name w/ Multi Names and & sign

Postby Glenn » Sun Oct 09, 2011 7:12 pm

Here's a quick and dirty solution that works with the 2 filenames given. You may need to tweak it to work with variations.
Try the following:
Regex(1)
Match: (\w+?), (.+?)(&|-) ((\w+?),(.+?)(&|-))*(.+)

Replace: \2\1 \3\6\5 \7\8

Note that there is a space after the & and the - in (&|-)

Make sure Include Ext is Unchecked!

Capturing groups \5-\8 don't contain anything when dealing with filenames containing only one author, so they don't add anything to the replacement for these.

If you have more than 2 authors, you need to add the repeating section ((\w+?),(.+?)(&|-))*, one for each additional author.
For example, for one to three authors
to end up with (\w+?), (.+?)(&|-) ((\w+?),(.+?)(&|-))*((\w+?),(.+?)(&|-))*(.+)

and change the replacement string to \2\1 \3\6\5 \7\8\10\9\11\12.

There are shorter ways of doing this but they are relatively more complex and more difficult to explain.

Hope this hasn't muddied the waters too much.

Cordially,
Glenn
Glenn
 
Posts: 28
Joined: Fri Apr 14, 2006 4:53 pm
Location: Winnipeg, Canada


Return to Regular Expressions