How to replace character based on pattern match

Bulk Rename Utility How-To's

How to replace character based on pattern match

Postby Lappen » Mon Nov 01, 2021 1:14 pm

I have files / Folders with multiple "." that i want to change to a "whitespace" unless its a 4 digit number then i would like to replace .dddd. to "whitespace"(dddd) and then remove the rest of the name

For Example

Lappen.Dinner.1972.Strange

I would like to change to

Lappen Dinner (1972)
Lappen
 
Posts: 5
Joined: Mon Nov 01, 2021 1:06 pm

Re: How to replace character based on pattern match

Postby therube » Mon Nov 01, 2021 4:17 pm

I'd do it in two parts.
As a start...

First handle the \d\d\d\d.

1:RegEx:
Code: Select all
Match:  (.*?)\.(\d\d\d\d)
Replace:  \1


3:Replace:
Code: Select all
Replace:  .
With:  <sp>

Note that ".strange" is the file extension (if you will), so results may vary depending on
Renaming Options | File/Folder Extensions, settings.

Also be aware of names that contain multiple sets of or more then 4 digits, like:
Lappen.Dinner.1972.Stranger.In.2021 or TV.series.90210 (five digits)


Second:

3:Replace:
Code: Select all
Replace:  .
With:  <sp>


(<sp> is a physical space, not "sp")
therube
 
Posts: 1314
Joined: Mon Jan 18, 2016 6:23 pm

Re: How to replace character based on pattern match

Postby Lappen » Mon Nov 01, 2021 6:12 pm

Thanx, a bit on the way but the suggested code removes the year between dots instead of adding ()
Lappen
 
Posts: 5
Joined: Mon Nov 01, 2021 1:06 pm

Re: How to replace character based on pattern match

Postby Lappen » Mon Nov 01, 2021 6:15 pm

Lappen wrote:Thanx, a bit on the way but the suggested code removes the year between dots instead of adding ()
https://www.dropbox.com/s/5vs5s4dx19bu46w/Capture.PNG
Lappen
 
Posts: 5
Joined: Mon Nov 01, 2021 1:06 pm

Re: How to replace character based on pattern match

Postby Lappen » Mon Nov 01, 2021 6:30 pm

therube wrote:I'd do it in two parts.
As a start...

First handle the \d\d\d\d.

1:RegEx:
Code: Select all
Match:  (.*?)\.(\d\d\d\d)
Replace:  \1


3:Replace:
Code: Select all
Replace:  .
With:  <sp>

Note that ".strange" is the file extension (if you will), so results may vary depending on
Renaming Options | File/Folder Extensions, settings.

Also be aware of names that contain multiple sets of or more then 4 digits, like:
Lappen.Dinner.1972.Stranger.In.2021 or TV.series.90210 (five digits)


Second:

3:Replace:
Code: Select all
Replace:  .
With:  <sp>


(<sp> is a physical space, not "sp")


Thanx you got me on the right track, modified the code like this

1:RegEx:
Code: Select all
Match:  (.*?)\.(\d\d\d\d)
Replace:  \1 (\2)


3:Replace:
Code: Select all
Replace:  .
With:  <sp>
Lappen
 
Posts: 5
Joined: Mon Nov 01, 2021 1:06 pm

Replace dots with space (if before .dddd.)

Postby Luuk » Mon Nov 01, 2021 9:47 pm

The 'v2' checkmark in RegEx does also grant saying many different Match/Replace expressions inbetween (?X) like...
\.(?=.*\.\d{4}\.)/g(?X)\.(\d{4})\..*
\ (?X) \($1\)

The 1st Match/Replace converts all periods ========> "space" (except .dddd. and everything after).
The 2nd Match/Replace converts .dddd.AnyThing ===> " (dddd)"
So renaming them like...

Lappen.Dinner.1972.Strange =================> Lappen Dinner (1972)
Some.Very.Long.Name.1980.To.Be.Removed ====> Some Very Long Name (1980)
Some.Other.Very.Long.1980-Name ============> (not renamed, because no .dddd. inside name)

Inside Match1, the /g just says global, so this will replace all matches of "." that are found.
The ?= starts a lookahead group, to make sure that .dddd. comes after all dots that are found.
Luuk
 
Posts: 692
Joined: Fri Feb 21, 2020 10:58 pm

Re: Replace dots with space (if before .dddd.)

Postby Lappen » Tue Nov 02, 2021 7:40 am

Luuk wrote:The 'v2' checkmark in RegEx does also grant saying many different Match/Replace expressions inbetween (?X) like...
\.(?=.*\.\d{4}\.)/g(?X)\.(\d{4})\..*
\ (?X) \($1\)

The 1st Match/Replace converts all periods ========> "space" (except .dddd. and everything after).
The 2nd Match/Replace converts .dddd.AnyThing ===> " (dddd)"
So renaming them like...

Lappen.Dinner.1972.Strange =================> Lappen Dinner (1972)
Some.Very.Long.Name.1980.To.Be.Removed ====> Some Very Long Name (1980)
Some.Other.Very.Long.1980-Name ============> (not renamed, because no .dddd. inside name)

Inside Match1, the /g just says global, so this will replace all matches of "." that are found.
The ?= starts a lookahead group, to make sure that .dddd. comes after all dots that are found.


Thanx for the detailed explanation!
Lappen
 
Posts: 5
Joined: Mon Nov 01, 2021 1:06 pm


Return to How-To