Rename with Match and Replace

A swapping-ground for Regular Expression syntax

Rename with Match and Replace

Postby pruebez » Thu Mar 19, 2020 9:22 pm

I want to ask something simple before asking something more complicated.
I have a file named "test [1234].txt"

If I:
Code: Select all
Match: 1234
Replace: hi


It results in "hi.txt" instead of the expected "test [hi].txt"
Am I doing something wrong?
pruebez
 
Posts: 4
Joined: Thu Mar 19, 2020 9:18 pm

Re: Rename

Postby RegexNinja » Fri Mar 20, 2020 9:50 am

Hi,

In BRU, you have to (Group) everything you wanna keep inside parenthesis.
Anything not grouped will be lost (to let you replace it, if desired).
Just use \1, \2, \3, etc to reference your (Groups) in the replacement.

So the Match/Replace is:

(.*)1234(.*)
\1hi\2


Much more data in the new manual: http://www.bulkrenameutility.co.uk/foru ... =12&t=4743
Its material is suitable for beginners and experts alike.
Cheers!
RegexNinja
 
Posts: 134
Joined: Fri Feb 21, 2020 5:26 pm

Re: Rename with Match and Replace

Postby Admin » Sat Mar 21, 2020 1:12 am

BTW if you just need to replace a string of text with another string of text, you can use Replace (3) in BRU.
Admin
Site Admin
 
Posts: 1848
Joined: Tue Mar 08, 2005 8:39 pm

Re: Rename with Match and Replace

Postby RegexNinja » Sat Mar 21, 2020 6:45 am

Yes, I should've mentioned #3Replace! Soo used to regexing lately, lol.
The good part about #3Replace, is that it replaces all occurences of 1234.

With #1Regex, that's more difficult to achieve.
Cheers.
RegexNinja
 
Posts: 134
Joined: Fri Feb 21, 2020 5:26 pm

Re: Rename with Match and Replace

Postby pruebez » Thu Apr 02, 2020 2:01 pm

That was just an example, I wanted to remove a tag of 8 hex digits in between [] from a bunch of files ( ex: myvideo[14c5aa3c]).mkv)
So replace didnt work.
pruebez
 
Posts: 4
Joined: Thu Mar 19, 2020 9:18 pm

Re: Rename with Match and Replace

Postby RegexNinja » Thu Apr 02, 2020 3:20 pm

Lol, been wondering if that 'more complicated' was gonna show up.. That's definitely a job for regex.
Here's one to that's case-insensitive to remove the 1st-occurence of [Any8HexDigits]:

#1Regex Match/Replace
(?i)(.*?)\[([a-f0-9]{8})\](.*)
\1\3

Results:
myvideo[14c5aA3c]) -----> myvideo)
Begin[123aBcDF]End -----> BeginEnd
file[1111111G] -----------> no effect, not hex
RegexNinja
 
Posts: 134
Joined: Fri Feb 21, 2020 5:26 pm

Re: Rename with Match and Replace

Postby RegexNinja » Thu Apr 02, 2020 4:03 pm

Here's a javascript that's case-insensitive to remove all-occurences of [Any8HexDigits]

newName=name.replace(/\[[0-9a-f]{8}\]/ig,'')

In both cases, you can just change the CharSet from [0-9a-f] to [0-9a-fA-F] to avoid invoking case-insensitivity.
Cheers.
RegexNinja
 
Posts: 134
Joined: Fri Feb 21, 2020 5:26 pm

Re: Rename with Match and Replace

Postby pruebez » Wed Apr 08, 2020 1:01 am

What does (?i) do?

And whats the difference between (.*?) and (.*) ?
pruebez
 
Posts: 4
Joined: Thu Mar 19, 2020 9:18 pm

Re: Rename with Match and Replace

Postby RegexNinja » Wed Apr 08, 2020 9:39 am

(?i) invokes case-insensitivity to all that follows, unless turned off with (?-i)
So to match any 8-digit hex-string with upper/lowers, you could use either:
(?i)([0-9a-f]{8})(?-i) -or- ([0-9a-fA-F]{8})

(.*?)XX matches everything till the 1st-occurrence of XX
(.*)XX matches everything till the last-occurrence of XX
RegexNinja
 
Posts: 134
Joined: Fri Feb 21, 2020 5:26 pm

Re: Rename with Match and Replace

Postby pruebez » Mon Apr 13, 2020 5:44 pm

Thanks. Sure its complicated but powerful.
pruebez
 
Posts: 4
Joined: Thu Mar 19, 2020 9:18 pm


Return to Regular Expressions