Regex to add ) in filename also removes last 4 chararacters

Bulk Rename Utility How-To's

Regex to add ) in filename also removes last 4 chararacters

Postby SeismicVoice » Fri Jun 09, 2023 6:03 pm

Sorry to post again so soon but I really think this will be the last one for a good while.

I want a regex that will add ) immediately after the 5th from last word in a filename

I have the following regex
Code: Select all
Match:
^(.*) ([^ ]+ [^ ]+ [^ ]+ [^ ])
Replace:
\1) \2

with no boxes clicked


What I want it to do is take filenames like:

1stword 2ndword 3rdword 4thword 5thword 6thword 7thword 8thword 9thword.ext
1stword 2ndword 3rdword 4thword 5thword 6thword.ext
1stword 2ndword 3rdword 4thword 5thword 6thword 7thword 8thword.ext


and change them like so:

1stword 2ndword 3rdword 4thword 5thword) 6thword 7thword 8thword 9thword.ext
1stword 2ndword) 3rdword 4thword 5thword 6thword.ext
1stword 2ndword 3rdword) 4thword 5thword 6thword 7thword 8thword.ext


What I have right now does put the ) in the right position but it also removes the last 4 characters of a filename so that I end up with:

1stword 2ndword 3rdword 4thword 5thword) 6thword 7thword 8thword 9th.ext
1stword 2ndword) 3rdword 4thword 5thword 6th.ext
1stword 2ndword 3rdword) 4thword 5thword 6thword 7thword 8th.ext


Can you help me so that only the ) is added in the correct position but no characters are removed? Thanks
SeismicVoice
 
Posts: 4
Joined: Fri Jun 09, 2023 4:23 am

Add ) before " Last4Words"

Postby Luuk » Sat Jun 10, 2023 3:01 am

Its because the last [^ ] is missing the + to say 1-or-more, so that he's only matching exactly 1-character.
When RegEx(1) doesnt have any checkmarks, any extra-text that's not getting matched, gets removed!!
The best way I like thinking of it, is that .* gets added the start/end, when there's not any ^/$ in the match.

So without any of the RegEx(1) checkmarks, its usually better to always use both "^" and "$" inside of your "Match".
So then, the non-matched text cant get removed, and any non-matched names can show why the match isnt exact.

When using the "v2" checkmark, then RegEx(1) will only modify exactly what's inside of the "Match".
But its "Replace" needs "\(" to say "(" and should use $GroupNumbers instead of \GroupNumbers.
So really whichever is the easiest to modify and remember or maybe to save?

For myself only, I would probably use "v2" with something like...
(?<!\))(?=( [^ ]+){4}$)
\(

The 1st-group is a negative look-behind, to match a position where there's not any ) to-the-left.
The 2nd-group is a look-ahead, to match a position where there's 4-words to-the-right at the very end.
Since the look-arounds only match positions inbetween the text, the "Replace" doesnt need any groups.
Luuk
 
Posts: 706
Joined: Fri Feb 21, 2020 10:58 pm

Re: Add ) before " Last4Words"

Postby SeismicVoice » Sat Jun 10, 2023 1:54 pm

Luuk wrote:So really whichever is the easiest to modify and remember or maybe to save?

For myself only, I would probably use "v2" with something like...
(?<!\))(?=( [^ ]+){4}$)
\(

The 1st-group is a negative look-behind, to match a position where there's not any ) to-the-left.
The 2nd-group is a look-ahead, to match a position where there's 4-words to-the-right at the very end.
Since the look-arounds only match positions inbetween the text, the "Replace" doesnt need any groups.


I very much appreciate you taking the time to provide a solution and an explanation. Thank you! And I'm definitely keeping the various regexs I use in a file for future reference.
SeismicVoice
 
Posts: 4
Joined: Fri Jun 09, 2023 4:23 am


Return to How-To