Move last word to somehwere else (separated by _)

Bulk Rename Utility How-To's

Move last word to somehwere else (separated by _)

Postby shmellyman » Sun Dec 15, 2024 4:59 pm

Sorry for double posting, but another question. I've been using "%1_%2_%3_%4_%5" in match and replace in reg (1) to move words around. I find it really flexible and ituitive. But lets say i have a bunch of files and each file has different amount of words. I would I go about moving the last word of each file to in between words 3 and 4 for example? would I still be using "%1_%2_%3_%4_%5" somehow?

Another question, how would i use "%1_%2_%3_%4_%5" on files with more than 10 words? using "%10" does not seem to work if i wanted to move the 10th word around. thanks.
shmellyman
 
Posts: 15
Joined: Thu May 06, 2021 9:13 am

Re: Move last word to somehwere else

Postby Admin » Mon Dec 16, 2024 1:29 am

Hi

Sorry for double posting, but another question. I've been using "%1_%2_%3_%4_%5" in match and replace in reg (1) to move words around. I find it really flexible and ituitive. But lets say i have a bunch of files and each file has different amount of words. I would I go about moving the last word of each file to in between words 3 and 4 for example? would I still be using "%1_%2_%3_%4_%5" somehow?


It gets a little more complicated... you will need to use RegEx (1) without "Simple" enabled and with v2 flag active.

You have a name such as: 1_2_3_4_5_6
You want to move the last word 6 after 3.

Use
Match: (.*?_3_)(.*?)_([^_]+)$
Replace: \1\3_\2

Explanation:

Pattern: Use a capturing group to identify the parts of the string:
(.*?_3_)(.*?)_([^_]+)$

Breakdown:
.*?_3_ matches everything up to _3_ (lazy match to ensure _3_ is included).
(.*?) matches everything between _3_ and the last underscore (non-greedy).
([^_]+)$ captures the last part of the string after the final underscore.

Substitution:
Replace with \1\3_\2, which reorders the groups:
\1 is the part before _3_.
\3 is the last word.
\2 is the part that was originally after _3_.

Another question, how would i use "%1_%2_%3_%4_%5" on files with more than 10 words? using "%10" does not seem to work if i wanted to move the 10th word around. thanks.


Only 9 groups are supported with "Simple" RegEx in BRU.
Admin
Site Admin
 
Posts: 2883
Joined: Tue Mar 08, 2005 8:39 pm

Re: Move last word to somehwere else

Postby shmellyman » Mon Dec 16, 2024 5:00 am

Admin wrote:Hi

Sorry for double posting, but another question. I've been using "%1_%2_%3_%4_%5" in match and replace in reg (1) to move words around. I find it really flexible and ituitive. But lets say i have a bunch of files and each file has different amount of words. I would I go about moving the last word of each file to in between words 3 and 4 for example? would I still be using "%1_%2_%3_%4_%5" somehow?


It gets a little more complicated... you will need to use RegEx (1) without "Simple" enabled and with v2 flag active.

You have a name such as: 1_2_3_4_5_6
You want to move the last word 6 after 3.

Use
Match: (.*?_3_)(.*?)_([^_]+)$
Replace: \1\3_\2

Explanation:

Pattern: Use a capturing group to identify the parts of the string:
(.*?_3_)(.*?)_([^_]+)$

Breakdown:
.*?_3_ matches everything up to _3_ (lazy match to ensure _3_ is included).
(.*?) matches everything between _3_ and the last underscore (non-greedy).
([^_]+)$ captures the last part of the string after the final underscore.

Substitution:
Replace with \1\3_\2, which reorders the groups:
\1 is the part before _3_.
\3 is the last word.
\2 is the part that was originally after _3_.

Another question, how would i use "%1_%2_%3_%4_%5" on files with more than 10 words? using "%10" does not seem to work if i wanted to move the 10th word around. thanks.


Only 9 groups are supported with "Simple" RegEx in BRU.


Thanks for the solution, but this seems to only work if the the third word is named "3". What if the 3rd word is named differently for every file?
shmellyman
 
Posts: 15
Joined: Thu May 06, 2021 9:13 am

Re: Move last word to somehwere else

Postby Admin » Mon Dec 16, 2024 7:08 am

Try this

Pattern:

^((?:[^_]+_){3})(.*?)(_([^_]+))$

Breakdown:
^ asserts the start of the string.
((?:[^_]+_){3}) matches the first 3 words, including their trailing underscores.
(.*?) matches everything between the 4th word and the last word.
(_([^_]+))$ matches the last word (and its preceding underscore).

Substitution:

Replace with \1\4_\2:

\1: Captures the first 3 words (position 1, 2, and 3).
\4: Captures the last word, to be inserted at position 4.
\2: Captures everything after position 3 up to (but excluding) the last word.
Admin
Site Admin
 
Posts: 2883
Joined: Tue Mar 08, 2005 8:39 pm

Re: Move last word to somehwere else

Postby Admin » Mon Dec 16, 2024 7:11 am

With a Javascript function:

Code: Select all
// Split the string into an array of words
parts = name.split("_");

// Remove the last word
lastWord = parts.pop();

// Insert the last word into the 4th position (index 3)
parts.splice(3, 0, lastWord);

// Join the array back into a string
newName = parts.join("_");
Admin
Site Admin
 
Posts: 2883
Joined: Tue Mar 08, 2005 8:39 pm


Return to How-To