Moving the last 2 words of any phrase

A swapping-ground for Regular Expression syntax

Moving the last 2 words of any phrase

Postby danger007 » Sun Jan 24, 2016 8:25 am

I am trying to move around the last 2 words of any phrase. There are no markings/delimiters like - ! #, so here is an example

2015-01-01 - Market Center Alpha San Jose

I would like to appear as

2015-01-01 - San Jose - Market Center Alpha.zip

Another Example

2015-01-10 - PRIMARY PUBLIC EDUCATION MATERIALS TWIN C.zip
I would like it to appear as
2015-01-10 - TWIN C - PRIMARY PUBLIC EDUCATION MATERIALS.zip

So in general
Date - [Group of Words before the last 2] [last 2 words have space in between].extension
Note: [next to last last] would have a space between the last two words like: San Jose, Cabinet 1, Disc AB
2015-01-10 - [word 1 word 2 word 3 or more] [next to last last]
into
2015-01-10 - [next to last last] - [word 1 word 2 word 3 or more].zip

I have tried several expressions but can't seem to find the correct way to tell it to grab both of the last 2 words and move them. Maybe this can't be done in the manner I am trying, so I could live with:
San Jose 2015-01-01 Market Center Alpha.zip
Since an expression like Match (.*) (.*) (.*) - (.+) would then make it Replace: \3 - \1 \2 - \3
2015-01-01 - San Jose - Market Center Alpha

Thanks for any help.

My Skill Level - I would consider you are speaking to a new user versus someone who has normal comprehension of programming (I have used regex but not to the advanced level formatting I am trying) - hopefully this will help you in knowing what to say in the response, thanks again.
danger007
 
Posts: 1
Joined: Sun Jan 24, 2016 8:10 am

Re: Moving the last 2 words of any phrase

Postby RobsterUK » Mon Jan 25, 2016 11:01 am

I'm quite new to RegEx myself and just learning so this may not work for all files but it did for the two examples you posted.

Match:
(.+)-(.+) (\w+) (\w+)$

Replace:
\1- \3 \4 -\2

2015-01-01 - Market Center Alpha San Jose.zip

becomes

2015-01-01 - San Jose - Market Center Alpha.zip

(.+)- matches everything up to the first dash "-"
(.+) Matches everything up until the last two matches
(\w+) matches individual words
$ indicates the end of the filename, so therefore forces the previous two matches (\w+) to only match the last two words.
Hope that makes sense....

Anyway try the code and see how you get on!
RobsterUK
 
Posts: 8
Joined: Wed Jan 20, 2016 12:00 pm


Return to Regular Expressions