MRT: need help with RegEx: rename to remove this but keep that?

English support forum

Moderators: white, Hacker, petermad, Stefan2

Post Reply
User avatar
wanderer
Power Member
Power Member
Posts: 1578
Joined: 2003-03-28, 14:35 UTC
Location: Sol

MRT: need help with RegEx: rename to remove this but keep that?

Post by *wanderer »

Original Subject was: RegEx help

I need some help with regex. TC help or http://regexpstudio.com/en/regexp_syntax.html were not much help, so...

I have some files in several folders, which are of format "blah1 (blah2) blah3 (blah4)". I would like to remove "(blah2) " from all of them. In every folder, blah2 differs, so i'm using MRT to mass-rename all of them. "blah4" i want to keep. This is usually 1-3 chars long while "blah2" is always 5+.

I tried using "\(.\)" as regex (which i thought would do the trick), but it was not helpful. It would remove some instances of "blah4" (the ones with 1 char). "\(*\)" was also not helpful.

Could someone please give me an example at first on how to successfully escape the parenthesis and then to tell MRT that i would like to replace only text within parenthesis with length greater than a specific number of chars?
- Wanderer -

¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯
Normally using latest TC on:
x32: WinXPx32 SP3
x64: Clients/Servers from Win7 to Win11 and Win2K12Srv to Win2K22Srv, mainly Win10 though.
User avatar
wanderer
Power Member
Power Member
Posts: 1578
Joined: 2003-03-28, 14:35 UTC
Location: Sol

Re: RegEx help

Post by *wanderer »

OK, never mind, i got it. I used " \(.{5,}?\)". It's confusing though. Depending on the characters you put inside the escaped parenthesis, regex may decide NOT to escape the parenthesis and doesn't replace them. Makes you think you're doing something wrong...
- Wanderer -

¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯
Normally using latest TC on:
x32: WinXPx32 SP3
x64: Clients/Servers from Win7 to Win11 and Win2K12Srv to Win2K22Srv, mainly Win10 though.
User avatar
Stefan2
Power Member
Power Member
Posts: 4157
Joined: 2007-09-13, 22:20 UTC
Location: Europa

Re: RegEx help

Post by *Stefan2 »

FROM:
bl1 (bl2) blah3 (blah4).txt
blah1 (blah2) blah3 (blah4).txt
blaha1 (blaha2) blah3 (blah4).txt
blahab1 (blahab2) blah3 (blah4).txt
blahabc1 (blahab c2) blah3 (blah4).txt

TO:
bl1 blah3 (blah4).txt
blah1 blah3 (blah4).txt
blaha1 blah3 (blah4).txt
blahab1 blah3 (blah4).txt
blahabc1 blah3 (blah4).txt

MRT:
Search: ^(.+?)\s\(.+?\)
Replace: $1
[X]RegEx


Explanation:
^ >>> work on start of string.
(.+?) >>> match one-or-more of any sign non-greedy till next expression and save in capture group (...) number one.
\s\(.+?\) >>> match a space and a literal '(' plus match one-or-more of any sign non-greedy till next ')'.
Replace by capture group (...) number one>>> '$1'.

Rest of string is untouched in TC regex engine.

RegEx Syntax:
. >>> any sign
+ >>> match one-or-more of the expression right before
* >>> match zero-or-more of the expression right before
(...) >>> store in a capture group
? >>> match non-greedy instead of 'the last possible match'

 
User avatar
wanderer
Power Member
Power Member
Posts: 1578
Joined: 2003-03-28, 14:35 UTC
Location: Sol

Re: RegEx help

Post by *wanderer »

Hey Stefan2, thanks for the info. I'll try those out when i get on a pc.
- Wanderer -

¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯
Normally using latest TC on:
x32: WinXPx32 SP3
x64: Clients/Servers from Win7 to Win11 and Win2K12Srv to Win2K22Srv, mainly Win10 though.
Post Reply