skip 1st 8 chars, replace subsequent hyphens with whitespace

A swapping-ground for Regular Expression syntax

skip 1st 8 chars, replace subsequent hyphens with whitespace

Postby cobengo » Sat Apr 16, 2022 6:28 pm

I've a large batch of video files containing one or more hyphens. I normally use "Replace" function to strip all hyphens & manually re-add as needed to properly index multi-episode files. However, the number of files to be renamed makes this approach impractical.

I would like to retain the 1st 8 characters of filename unchanged, and replace subsequent hyphens with single whitespace.

sample:
S01 E01-E03 titlename01 - titlename02 - titlename03

desired output:
S01 E01-E03 titlename01 titlename02 titlename03

Thanks for any assistance here!
cobengo
 
Posts: 4
Joined: Sat Apr 16, 2022 5:34 pm

Re: skip 1st 8 chars, replace subsequent hyphens with whitespace

Postby therube » Tue Apr 19, 2022 8:40 pm

One method...

3:Replace
Code: Select all
Replace:  \second\-|\third\-
With:  <sp>

5:Remove -> D/S

Actually, that is not working when you include the OR (|) in there.
Wonder why? One (at least, I) would assume that it should.

---

Try this:

1:RegEx -> Simple (enable)

Code: Select all
Match:  %1-%2-%3-%4
Replace:  %1-%2 %3 %4


5:Remove -> D/S

Match everything up to -
Match everything up to -
Match everything up to -
Match everything else

replace with

everything1 AND -
everything2
everything3
everything4

(Except for the first - in everything1, all other dashes were excluded [in the Replace:].)
therube
 
Posts: 1314
Joined: Mon Jan 18, 2016 6:23 pm

Re: skip 1st 8 chars, replace subsequent hyphens with whitespace

Postby cobengo » Wed Apr 20, 2022 12:32 am

Thanks for your suggestions - much better that manual delete/add. The second Match/Replace below) works for files with exactly 3 hyphens.

Code: Select all
Match:  %1-%2-%3-%4
Replace:  %1-%2 %3 %4


Unfortunately, the number of hyphens in each file varies across the file set. I can make this work by changing the number of groups in the match code as needed, but would appreciate any further sugestions toward a single-step solution.
cobengo
 
Posts: 4
Joined: Sat Apr 16, 2022 5:34 pm

Re: skip 1st 8 chars, replace subsequent hyphens with whitespace

Postby cobengo » Wed Apr 20, 2022 12:46 am

Just realized another exception - 1st hyphen will always be retained with current Match/Replace, and desired result is to only keep hyphen occurring within 1st 8 character positions.
cobengo
 
Posts: 4
Joined: Sat Apr 16, 2022 5:34 pm

Re: skip 1st 8 chars, replace subsequent hyphens with whitespace

Postby Luuk » Fri Apr 29, 2022 11:42 pm

This can also depend if you need to rename some files like in the description, or if its only " - " that should ever get replaced??
The way this conducts can be hard to describe, so Im just giving some "Now-Names" ====> "New-Names" to present the changes.

To skip 8-characters, then replace " - " with space, the RegEx(1) needs a checkmark in "v2" with a "Match" and "Replace" like...
(\G(?!^)|^.{8}).*?\K +- +/g
\x20
S01-E01-E03 name1-name2.txt ================> (no change, because no " - ")
S01-E01-E03 name1-name2-name3-name4.txt ===> (no change, because no " - ")
S01-E01-E03 name1 - name2 - name3.txt =======> S01-E01-E03 name01 name02 name03.txt

To skip 8-characters, then replace both "-" and " - " with space, the "Match" and "Replace" can be more like...
(\G(?!^)|^.{8}).*?\K *- */g
\x20
S01-E01-E03 name1-name2.txt ================> S01-E01-E03 name1 name2.txt
S01-E01-E03 name1-name2-name3-name4.txt ===> S01-E01-E03 name1 name2 name3 name4.txt
S01-E01-E03 name1 - name2 - name3.txt =======> S01-E01-E03 name1 name2 name3.txt

Its unfortunate, but the forum software will not grant saying any lines with only having a space.
So Im just using '\x20' to say 'space', but you can really just type a space for the replacements.
Luuk
 
Posts: 692
Joined: Fri Feb 21, 2020 10:58 pm

Re: skip 1st 8 chars, replace subsequent hyphens with whitespace

Postby cobengo » Sat Apr 30, 2022 6:30 pm

Thanks so much for your help, this works great!

I'm using the second example for match/replace, as I wish to replace all hyphens with whitespace, regardless of adjacent character.

(\G(?!^)|^.{8}).*?\K *- */g
\x20

Filter mask below displays tv series episodes containing hyphens, and above match/replace strips hyphens found after 1st 8 positions

^S\d.+-
cobengo
 
Posts: 4
Joined: Sat Apr 16, 2022 5:34 pm


Return to Regular Expressions