Character replace within a match group (component)

A swapping-ground for Regular Expression syntax

Character replace within a match group (component)

Postby szillitto » Thu Sep 09, 2021 5:28 pm

Is it possible to do a replace of a character but only within a regex match group (component)?

Current = Jeremy-Irons-Margin-Call-Chapter-4-Untouched-DVDSource-EasyRip-240121.mp4
Desired = Jeremy Irons Margin Call Chapter 4 - Untouched - DVDSource-EasyRip-240121.mp4

Using this regex:
Match = (^.*)(-Untouched-)(.*$)
Replace = \1 - Untouched - \2

I am able to break out the match groups to use the Replace, but not sure how to use regex in the Replace field to replace the hyphens only in the first group.
szillitto
 
Posts: 3
Joined: Thu Sep 09, 2021 4:33 pm

Re: Character replace within a match group (component)

Postby Luuk » Thu Sep 09, 2021 6:03 pm

Inside of 'Replace', you can just change \2 ==> \3 since the 'Match' is using 3 groups of parenthesis.
Or you could just move both of the '-' outside of group-2, so then using a 'Match' and 'Replace' like...
^(.+)-(Untouched)-(.+)
\1 - \2 - \3
Luuk
 
Posts: 318
Joined: Fri Feb 21, 2020 10:58 pm

Re: Character replace within a match group (component)

Postby szillitto » Thu Sep 09, 2021 6:41 pm

Thank you Luuk! I messed up and meant that I am using:
Replace = \1 - Untouched - \3

However, how do I remove the hyphens only within Group-1?

So your solution:
^(.+)-(Untouched)-(.+)
\1 - \2 - \3

gets me this:
Jeremy-Irons-Margin-Call-Chapter-4 - Untouched - DVDSource-EasyRip-240121.mp4

but how do I get this?:
Jeremy Irons Margin Call Chapter 4 - Untouched - DVDSource-EasyRip-240121.mp4
szillitto
 
Posts: 3
Joined: Thu Sep 09, 2021 4:33 pm

Re: Character replace within a match group (component)

Postby Luuk » Thu Sep 09, 2021 10:00 pm

Many apologies, Im not even noticed this part about the explanation, so now Im thinking its better to use a 'v2' regex.
The "v2" regexs can let you use many different Match/Replace expressions by putting (?X) inbetween them like...

-(?=.+-Untouched-.+)/g(?X)-(Untouched)-
\ (?X) - $1 -

The 1st match/replace replaces all hyphens ===> 'Space' if they are anywhere before -Untouched-.
The 2nd match/replace replaces -Untouched- ==> ' - Untouched - '.

The 'v2' expressions dont need to match the whole filename, so any parts not-matched are not-modified.
And its usually better to say $Group instead of \Group, because Im already finding some glitches if not.
But its always ok matching the whole filename, so like your example uses the "v2" like...

-(?=.+-Untouched-.+)/g(?X)(^.*)(-Untouched-)(.*$)
\ (?X)\1 - Untouched - \3

The 'Replace only starts with '\' because this forum does like to forbid any spaces that try to begin the lines.
The ?= starts a look-ahead group, but its not-capturing, so this why the 1st-replace doesnt need any $1 or \1.
So when Im saying anything not-matched is not-modified, I should probably saying anything not-captured.
Luuk
 
Posts: 318
Joined: Fri Feb 21, 2020 10:58 pm

Re: Character replace within a match group (component)

Postby szillitto » Thu Sep 09, 2021 10:26 pm

AHHHHH yes. Now I understand what a look-ahead group does and how you can use (?X). THANKS!
szillitto
 
Posts: 3
Joined: Thu Sep 09, 2021 4:33 pm


Return to Regular Expressions