Remove 01 - From The Beginning Of File Name Based On Keyword

A swapping-ground for Regular Expression syntax

Remove 01 - From The Beginning Of File Name Based On Keyword

Postby Shawnny Canuck » Tue Mar 14, 2017 10:27 pm

Hello,

I am trying to figure out the opposite of this RegEx (see below.) Instead of adding “01 -“ to the front of the file name based on a keyword match I want to remove the “01 -“ from the front of the file name based on a keyword match.

This is the RegEx to add “01 -“

FIND: ^(.+TITLE 2)$
REPLACE: 01 - \1

But I want my files to go

FROM:
01 - TITLE 1 - TITLE 2 - TITLE 3
TO:
TITLE 1 - TITLE 2 - TITLE 3

Thanks for your help.
Shawnny Canuck
 
Posts: 18
Joined: Wed Jul 30, 2014 7:15 pm

Re: Remove 01 - From The Beginning Of File Name Based On Keyword

Postby KenP » Tue Mar 14, 2017 10:51 pm

I don't really understand what you're trying to do, the regEx you've given certainly wouldn't add "01 - " to "Title 1 - Title 2 - Title 3".

Anyway based on what you've said and assuming the "TITLE 2" is the keywords you want to match this should work.

RegEx (1)
Match: 01 - (.*TITLE 2.*)
Replace: \1

If you could be a bit more explicit about what you're trying to do we may be able to be of more help :wink:
KenP
 
Posts: 199
Joined: Sat Jul 30, 2016 11:25 am

Re: Remove 01 - From The Beginning Of File Name Based On Keyword

Postby Shawnny Canuck » Wed Mar 15, 2017 4:34 am

Hi Ken,

My apologize I appreciate the help. Basically, a few years ago I ran a RegEx on a large amount of files and I added 01 - to the front of the file name if a keyword match was found in the string. Someone from this forum actually helped me with this. I used the RegEx given in my original post to do so and it worked so I am a little confused with your statement that it does not work. I also just tested it again in BRU without issue. My file format is as follows:

12345678 - TITLE 1 - TITLE 2

and after running the RegEx in my original post and using TITLE 2 as the keyword match my files were renamed to:

01 - 12345678 - TITLE 1 - TITLE 2

Now I am just trying to undo the change I made a few years ago as my requirements have changed. I understand this might seem a little odd but if you understood my business need for this it would make sense. After looking at your RegEx would I have been better off using:

FIND: (.*TITLE 2.*)
REPLACE: 01 - \1

I am terrible at RegEx so I am sure there is something I am not understanding. Thanks again for the help and I am really sorry for not giving more info and being so clueless.
Shawnny Canuck
 
Posts: 18
Joined: Wed Jul 30, 2014 7:15 pm

Re: Remove 01 - From The Beginning Of File Name Based On Keyword

Postby Shawnny Canuck » Wed Mar 15, 2017 4:35 am

Also, your example works beautifully thank you kindly :)
Shawnny Canuck
 
Posts: 18
Joined: Wed Jul 30, 2014 7:15 pm

Re: Remove 01 - From The Beginning Of File Name Based On Keyword

Postby KenP » Wed Mar 15, 2017 2:25 pm

Shawnny Canuck wrote:I used the RegEx given in my original post to do so and it worked so I am a little confused with your statement that it does not work. I also just tested it again in BRU without issue. My file format is as follows:

12345678 - TITLE 1 - TITLE 2

and after running the RegEx in my original post and using TITLE 2 as the keyword match my files were renamed to:

01 - 12345678 - TITLE 1 - TITLE 2

I'm sorry for the confusion but in your first post you only mentioned file names with the format "TITLE 1 - TITLE 2 - TITLE 3", the regex you gave ^(.+TITLE 2)$ would not work with that because your Regex requires the "TITLE 2" to be at the end of the file name.
The $ sign signifies the end of the file-name, so with "TITLE 2" immediately before the $ sign only file-names with "TITLE 2" at the end of the file-name will be matched.

It would however work with "TITLE 1 - TITLE 2" because in this case "TITLE 2" is at the end of the file name.

Shawnny Canuck wrote:After looking at your Regex would I have been better off using:

FIND: (.*TITLE 2.*)
REPLACE: 01 - \1

That depends on what you wanted.

The difference is that the Regex you used ^(.+TITLE 2)$ would only match a file-name with "TITLE 2" at the end of the file name, whereas the regex (.*TITLE 2.*) will match any file-name that contains "TITLE 2" in any position.
KenP
 
Posts: 199
Joined: Sat Jul 30, 2016 11:25 am

Re: Remove 01 - From The Beginning Of File Name Based On Keyword

Postby Shawnny Canuck » Wed Mar 15, 2017 4:24 pm

Got you. Sorry about that my file names are quite sensitive so I was trying to be generic as possible. Thanks again for your help and the explanation it makes a big difference when someone breaks things down and provides details on how and why it works a certain way.
Shawnny Canuck
 
Posts: 18
Joined: Wed Jul 30, 2014 7:15 pm


Return to Regular Expressions