Swap Two Words before [ ]

Bulk Rename Utility How-To's

Swap Two Words before [ ]

Postby KrystinSeo » Thu Feb 03, 2022 7:01 pm

Dear Bulk Rename,

I am writing to ask the proper expressions of swapping two words (first and last words before []) in a string:
I would like to swap the first word and the last word of a string before [] and keep the rest unchanged.

For example,
Case 1: Apple Seo [ABC] changes to Seo Apple [ABC].
Case 2: Apple Mango Seo [ABC] changes to Seo Apple Mango [ABC].

I went through the menus and found that the expression is useful only if the string is composed of 2 words. Could I know any possible solution for N words in a string?

Match
^([A-Z][a-z]*) ([A-Z][a-z]*) (.*)
Replace
\2 \1 \3

Your help is appreciated.
Seo.
KrystinSeo
 
Posts: 2
Joined: Thu Feb 03, 2022 6:54 pm

Swap 1st and last word before [ text]

Postby Luuk » Thu Feb 03, 2022 8:19 pm

With RegEx(1) the Match and Replace can be like...
^(?i)(([a-z]+? ){1,2})([a-z]+? )(\[[a-z]+\])$
\3\1\4

This to swap 1st and last-words before the ending [text] when there is only 1-or-2 words before the ending [text]...
Apple Seo [ABC] ===========> Seo Apple [ABC]
Apple Mango Seo [ABC] =====> Seo Apple Mango [ABC]
aaa bbb ccc ddd [ABC] ======> (no change, because too many words)

If needing to match more words before [text], can either increase the 2, or just change {1,2} ===> + to say "1-or-more".
Luuk
 
Posts: 705
Joined: Fri Feb 21, 2020 10:58 pm

Swap 1st and last word before [ text]

Postby Luuk » Thu Feb 03, 2022 8:40 pm

Its unfortunate but the description is confusing, what I mean is that {1,2} matches 1-or-2 words coming before the LastWord [text].
Luuk
 
Posts: 705
Joined: Fri Feb 21, 2020 10:58 pm

Re: Swap Two Words before [ ]

Postby Admin » Fri Feb 04, 2022 12:07 am

If the RegEx expression gets too complicated to match every different case that is present, another option could be to :

A)
- select all files with 2 words using Filter (12)
- apply regex to those (you could use simple regex too, e.g. %1 %2 [%3], replace with %2 %1 [%3]
B)
- select all files with 3 words using Filter (12)
- apply regex to those (you could use simple regex too, e.g. %1 %2 %3 [%4], replace with %1 %3 %2 [%4]

etc.
Admin
Site Admin
 
Posts: 2354
Joined: Tue Mar 08, 2005 8:39 pm

Re: Swap Two Words before [ ]

Postby KrystinSeo » Sat Feb 05, 2022 4:18 pm

Thank you!

Q1 Could I ask how to modify to cope with more than 1 word in [ ]?
AAA BBB CCC [DDD EEE] -------> CCC AAA BBB [DDD EEE]

Q2 Could I ask how to filter with respect to position of the words?

Thank you!
Seo
KrystinSeo
 
Posts: 2
Joined: Thu Feb 03, 2022 6:54 pm

Move last-word before ending [text] to the front

Postby Luuk » Sat Feb 05, 2022 7:14 pm

First Im apologize for the bad descriptions, because its not really swapping the "1st and last-word" before [text].
Its just moving the "last-word" to the front, and keeping everything else in order, like KrystinSeo does ask for.

For Q1, the RegEx(1) can use a "Match" and "Replace" like...
^(?i)(([a-z]+? ){1,2})([a-z]+? )(\[[^[]+\])$
\3\1\4

This to move LastWord if there is 1-or-2 words before "LastWord [any-text]" and any-text does not inlude "[".
Apple Seo [ABC] =============> Seo Apple [ABC]
Apple Mango Seo [ABC] =======> Seo Apple Mango [ABC]
Apple Mango CCC [ddd eee] ===> CCC Apple Mango [ddd eee]
aaa bbb ccc ddd [ABC] =======> (no change, because 3-words before LastWord is too many)

If changing {1,2} ===> {1,5} this moves LastWord if there is 1-thru-5 words before "LastWord [any-text]" like...
Apple Seo [ABC] =============> Seo Apple [ABC]
Apple Mango Seo [ABC] =======> Seo Apple Mango [ABC]
Apple Mango CCC [ddd eee] ===> CCC Apple Mango [ddd eee]
aaa bbb ccc ddd [ABC] ========> ddd aaa bbb ccc [ABC]
aa bb cc dd ee ff [gg hh] ======> ff aa bb cc dd ee [gg hh]
a b c d e f g h i [jj kk] ========> (no change, because 8-words before LastWord is too many)

For Q2, its unfortunate but I cannot understand this description without examples for which names to present or to filter?
To only present files with [2-or-more words] at the very end, the Filters(12) could use a "Mask" like \[.+ .+\]\.[^.]+$
Then you put a checkmark underneath for "RegEx", so only those names will be presented inside of the file-listing.
Luuk
 
Posts: 705
Joined: Fri Feb 21, 2020 10:58 pm


Return to How-To