flip lastname, firstname - title

A swapping-ground for Regular Expression syntax

flip lastname, firstname - title

Postby bobkoure » Wed May 30, 2007 4:59 pm

To convert
lastname, firstname - title
to
firstname lastname - tilte

use
^(\w+), *([\w \.]+)[ ]+-[ ]*(.*)
to
\2 \1 - \3

note - "[ ]*" could be " *" - just did it with brackets for clarity
bobkoure
 
Posts: 16
Joined: Mon Jul 10, 2006 5:38 pm

Postby Admin » Thu May 31, 2007 9:32 am

Great, many thanks for contributing. Other people will definitely find this useful.


Jim
Admin
Site Admin
 
Posts: 2343
Joined: Tue Mar 08, 2005 8:39 pm

Postby bobkoure » Thu May 31, 2007 2:46 pm

You're very welcome :-)

Looking at this again, it seems that a more generic requirement might be to change any of

lastname,firstname-title
lastname, firstname-title
lastname , firstname - title
etc. (basically possible spaces before/after the separator chars)
as well as
lastname, firstname initial.-title
including the above "possible space" variations

to get all of this, use
from
^(\w+)[ ]*,[ ]*([^-]+)[ ]*-[ ]*(.*)
to
\2\1 - \3

Again, "[ ]*" means "any number of spaces" and could be written " *" - but it's then harder to read...
bobkoure
 
Posts: 16
Joined: Mon Jul 10, 2006 5:38 pm

Postby Glenn » Fri Jun 01, 2007 4:16 pm

One minor point, and this would only be of concern if consistency in the spacing of the results is important.
If we start with the three examples:
lastname,firstname-title
lastname, firstname-title
lastname , firstname - title


Using the regex
^(\w+)[ ]*,[ ]*([^-]+)[ ]*-[ ]*(.*)

We end up with after the replacement

firstnamelastname - title
firstnamelastname - title
firstname lastname - title

Notice in the last example we have a space between the firstname and lastname.
This is because the second capturing group ([^-]+) will match anything except the "-" character; i.e. it will match a space, and because the [ ]* following it is optional you end up with a trailing space at the end of firstname. If this is an issue, change the second capturing group to disallow spaces - i.e. ([^- ]+)

It may be hard to see but there is a space added between the "-" and the "]"

Then the results would be:

firstnamelastname - title
firstnamelastname - title
firstnamelastname - title

This is the simplest solution - others would be needed if the firstname had spaces within it, but for the examples given this would do the trick.

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

Postby bobkoure » Tue Jun 05, 2007 2:45 pm

Right - should have mentioned that firstname could potentially have embedded spaces, so the only thing reliable was the comma and dash. I got lazy in listing all the possible variations.

Seems like the "thing to do" is just turn off "greediness" in that sub-expression, which'll let that trailing [ ]* "grab" any trailing spaces - and then of course, put a single space back in in the replacement - so:
from
^(\w+)[ ]*,[ ]*([^-]+?)[ ]*-[ ]*(.*)
to
\2 \1 - \3
Note that I have NOT TESTED this in BRU (at work - on deadline, not much time to play).

I'd thought about using this before, but wasn't sure if the regex engine in BRU supports +? and *? ... might just be an extension I've gotten used to using in Python.
I will post a question about the BRU regex engine.

Best,
Bob


PS, if you've got an improvement, I'd suggest you put the actual regex here - seems to me that there are non-regex-speaking folks reading this forum who are looking for something they can copy/paste into BRU and get a result.
bobkoure
 
Posts: 16
Joined: Mon Jul 10, 2006 5:38 pm

Re: flip lastname, firstname - title

Postby MJHJR » Tue Jan 06, 2009 10:52 pm

I just downloaded this utility and this is the first time I am going to use it.I would like to change .jpg photo files from first name to last name a few hundred at a time.For example:
Babe_Ruth.jpg to Ruth_Babe.jpg
Could someone be of help.
Thanks, Mike
MJHJR
 
Posts: 12
Joined: Tue Jan 06, 2009 10:45 pm

Re: flip lastname, firstname - title

Postby jimwillsher » Tue Jan 06, 2009 11:05 pm

Hi,

There's an example in the help file for doing precisely that; it swaps "Program Files" to "Files Program". You'll just need to work with an underscore instead of a space.



Jim
jimwillsher
 
Posts: 63
Joined: Wed Sep 24, 2008 1:22 pm

Re: flip lastname, firstname - title

Postby MJHJR » Thu Jan 08, 2009 7:06 pm

Thanks, Jim

Mike
MJHJR
 
Posts: 12
Joined: Tue Jan 06, 2009 10:45 pm

Re: flip lastname, firstname - title

Postby MJHJR » Fri Jan 16, 2009 8:41 pm

Hi Jim,
I guess I'm really stupid because I don't get it.
Could you write the expression out for me and what boxes to use?
I want to change several thousands of jpg files from first name to last name ( to sort them better).
An Example is: babe_ruth.jpg to Ruth_Babe.jpg
Thanks, Mike
[quote="jimwillsher"

There's an example in the help file for doing precisely that; it swaps "Program Files" to "Files Program". You'll just need to work with an underscore instead of a space.



Jim[/quote]
MJHJR
 
Posts: 12
Joined: Tue Jan 06, 2009 10:45 pm

Re: flip lastname, firstname - title

Postby GMA » Fri Jan 16, 2009 8:49 pm

Hi, MJHJR:
Use the following in the "RegEx (1)" field:

MATCH: (.*)_(.*)
REPLACE: \2_\1


Best regards,
Gabriel.
GMA
 
Posts: 91
Joined: Sun Dec 02, 2007 1:30 pm
Location: Argentina

Re: flip lastname, firstname - title

Postby MJHJR » Sat Jan 17, 2009 5:20 am

Gabriel,
Thanks a lot. It works and I actually can see how the thing works.

Thanks, Michael


GMA wrote:Hi, MJHJR:
Use the following in the "RegEx (1)" field:

MATCH: (.*)_(.*)
REPLACE: \2_\1


Best regards,
Gabriel.
MJHJR
 
Posts: 12
Joined: Tue Jan 06, 2009 10:45 pm

Re: flip lastname, firstname - title

Postby catdragon » Wed Jun 03, 2009 6:47 pm

I have a variation on this, but it doesn't seem to be working for me... can someone give a suggestion?

I have a number of files that look like this:

01. Mel & Kim - F.L.M..mp3
02. Damian - The Time Warp.mp3
04. Belouis Some - Some People.mp3
03. Huey Lewis & The News - The Power Of Love.mp3
05. Kim Wilde - Love Blonde.mp3

Now i want to switch the places of what's between the numbers and the ' - ' and between the ' - ' and the .mp3.

I tried the simple (.*) - (.*) => \2 - \1 but it didn't change anything. I'm going to keep trying stuff and if I have any success i will definitely post what i find here. But in the meantime, anyone have some suggestions?

Bobby
catdragon
 
Posts: 2
Joined: Wed Jun 03, 2009 6:36 pm

Re: flip lastname, firstname - title

Postby catdragon » Wed Jun 03, 2009 6:58 pm

Apparently I'm not very smart. :) I was putting my regular expression in the Repl. field instead of the RegEx field. I got it working now.

For the record, i used this as the RegEx (\d\d\.)\s(.*) - (.*) and changed it to \1 \3 - \2

Mea culpa....
catdragon
 
Posts: 2
Joined: Wed Jun 03, 2009 6:36 pm

Re: flip lastname, firstname - title

Postby ScorpiousDelectus » Wed Dec 02, 2009 5:58 pm

Hi guys and dolls,

First of all, amazing program! This has easily saved me about a month's worth of manual work, mainly because of the subfolder facility.

Anyway, I'm trying to do something very similar to the lastname, firstname swap but it's a little more complicated. I have files which are formatted in three different ways:

1) lastname, firstname - title - idcode
2) idcode - lastname, firstname, title
3) idcode - title - lastname, firstname

To make matters more complicated, sometimes there are more than one person in the name section or more a middle name as well: lastname, firstname middle name or lastname, firstname & firstname lastname etc etc

The RegEx code at the beginning of this thread works a treat in fixing the 1) example but doesn't obviously touch the other two. I've tried for the life of me to get my head around what the components of the code all mean and therefore how I can alter it to apply to 2) & 3) but I'm afraid it's beyond me.

Hopefully there's a few smart boffins in here that see it as easy as 1+1... :)
ScorpiousDelectus
 
Posts: 7
Joined: Wed Dec 02, 2009 5:50 pm

Re: flip lastname, firstname - title

Postby Stefan » Wed Dec 02, 2009 8:11 pm

Hi Scorpy,
and who should now pick up all the possible examples out of your text now?
You should had provide them like you did whit your first three examples in an list.
And you had shown how the new names should be.
Further you should give us a few real examples.
But you have to know that regular expressions is no magic wonder... it's just pattern matching - so you have to have any common pattern!
So this is the first step for you to do: bring all names to an common style, then you can think about an part swapping.
See this older threads for an RegEx syntax overview:
=> Getting Started: http://www.bulkrenameutility.co.uk/forum/viewtopic.php?f=3&t=5
=> Go ahead: http://www.bulkrenameutility.co.uk/forum/viewtopic.php?f=3&t=27
Stefan
 
Posts: 736
Joined: Fri Mar 11, 2005 7:46 pm
Location: Germany, EU

Next

Return to Regular Expressions