Swapping words in a file name

Advanced Renamer forum
#1 : 15/04-19 13:57
jchong
jchong
Posts: 5
Hi David Lee,

Thanks for helping with my last query. I'm still struggling with using regular expressions (sorry no programming background), so hope you can help.

I have files with longish names. There is no set number of words in the file name, it can vary.

Scenario 1: I want to swap the first 3 words:

aaa bbb ccc ddd eee fff
aaa bbb ccc ddd eee fff ggg

to

bbb ccc aaa ddd eee fff
bbb ccc aaa ddd eee fff ggg

Scenario 2: I want to swap the first 2 words:

aaa bbb ccc ddd eee fff
aaa bbb ccc ddd eee fff ggg

to

bbb aaa ccc ddd eee fff
bbb aaa ccc ddd eee fff ggg

Thanks in advance!


15/04-19 13:57
#2 : 15/04-19 19:34
David Lee
David Lee
Posts: 164
Regular expressions are tricky to get the hang of but generally fairly straightforward once you start to understand the logic. Start with Kim's simple introduction at www.advancedrenamer.com/user_guide/regular_expresions but for more complex information you will need to search the Internet - the Wikipedia article is a good place to start.

This problem is a little more tricky as "greedy" quantifiers will attempt to eat your entire string!

The following RegEx will do what you want (do not include the quotes)...

Problem 1: "^(\S+) (\S+ \S+)"
Problem 2: "^(\S+) (\S+)"

In each case use the replace string "\2 \1"

Explanation (prob 1)...

^ = start matching at the beginning of the filename

(\S+) = match as many non-whitespace characters as possible and save as sub-pattern group \1

<space> = match one space but don't save it

(\S+ \S+) = match two strings of non-whitespace chars separated by a space and save as \2

The replace string \2 \1 simply reverses the order of the two sub-pattern matches, separated by a space.

As I said, this is a bit more complex than Kim's simple examples and you will probably notice that the meta-character "\S" does not appear in Kim's very basic list (a similar meta-character "\s" is the opposite of "\S" and will match any whitespace character).


15/04-19 19:34 - edited 15/04-19 19:36
#3 : 16/04-19 01:52
jchong
jchong
Posts: 5
Reply to #2:

Thanks very much again. I did read Kim's introduction but was still not sure.

Glad you pointed out the meta-character \S.

I'm slowly getting the hang of it.


16/04-19 01:52
#4 : 22/04-19 09:13
JC
JC
Posts: 2
Reply to #2:

Many thanks for your answer. Advanced Renamer's regex is harder to search on the internet than others. Your answer came at just the right time for me too.


22/04-19 09:13