Regex help for renaming files
Regex help for renaming files
I'm having a crazy time trying to figure out regex and need some assistance. Im trying to rename a bunch of ebooks, heres an example.
A Clockwork Orange - Anthony Burgess.epub
and I would like to rename it as:
Anthony Burgess - A Clockwork Orange.epub
Is there any way I can group everything before the dash and after the dash?
Also is there a way to remove anything between two brakets? Like:
Zoey Dean - [A-List 06] - Some Like It Hot.epub
to be
Zoey Dean - Some Like It Hot.epub
Thanks for your time with this.
A Clockwork Orange - Anthony Burgess.epub
and I would like to rename it as:
Anthony Burgess - A Clockwork Orange.epub
Is there any way I can group everything before the dash and after the dash?
Also is there a way to remove anything between two brakets? Like:
Zoey Dean - [A-List 06] - Some Like It Hot.epub
to be
Zoey Dean - Some Like It Hot.epub
Thanks for your time with this.
Re: Regex help for renaming files
Match: ^(.*) - (.*)\.epub$
Replace: $2 - $1.epub
Replace: $2 - $1.epub
-
- Site Admin
- Posts: 64206
- Joined: 22 May 2004 16:48
- Location: Win8.1, Win10, Win11, all @100%
- Contact:
Re: Regex help for renaming files
So in RegExp Rename you would enter this:
Code: Select all
^(.*) - (.*)\.epub$ > $2 - $1.epub
FAQ | XY News RSS | XY X - Forum users with the Windows version and screen scaling percentage in the Location field of their profiles get priority support.
Re: Regex help for renaming files
Hi codyd, welcome.
Do you use XYplorer to rename?
FROM:
A Clockwork Orange - Anthony Burgess.epub
TO:
Anthony Burgess - A Clockwork Orange.epub
Match all till an dash followed by an space ==> "(.+) - "
Match the rest ==> "(.+)"
till an dot and the extension ===> "\.(.{3,4})"
Swap the order ==> "$2 - $1.$3"
Use XYplorer RegExReplace...
(.+) - (.+)\.(.{3,4}) > $2 - $1.$3
----------
Also is there a way to remove anything between two brakets? Like:
FROM:
Zoey Dean - [A-List 06] - Some Like It Hot.epub
TO:
Zoey Dean - Some Like It Hot.epub
Match all till an [ ==> "(.+)\["
Match all till an ] space dash space ==> ".+] - "
Match all to the end ==> "(.+)"
New output ==> "$1 $2"
Use XYplorer RegExReplace...
(.+)\[.+] - (.+) > $1$2
EDIT:
General notes/ Disclaimer
Hope this helps ?
If yes, please help two others too. And consider an donation to the tools autor.
Please note:
Always don't trust me! Test with test files first!
* Usually i do a few tests on this issue only!
* So please test my solution with some test files first before you destroy your data.
RegEx is an pattern matching solution, so all your files have to fit into the same pattern.
If they not, you have to separate them and run some more actions against them.
BTW: the terms "RegEx" or "RegExp" or "RE" are the abbreviation of this pattern matching concept (Regular Expressions)
and also used to call the regex pattern thyself.
My solution depends on the quality of your examples.
* It's always an good idea to provide all possibilities of file name pattern in question.
* That would give the supporter an change to do it right
* If your real file names doesn't fit into your example pattern my solution may fail.
So provide us always real live examples of your file names. (Of course you can sophisticate your secrets)
Hint:
* Don't use this ' ' or " " -quotes from my explanation. They are only for clarification.
* '?' means non-greedy: stop at first match, instead of last possible.
* This (...) parenthesis are used to "group" what is found by this RegEx inside the ( )'s
to reuse this capture later as replacement by using \1 or $1 (depending by the regex engine).
* Instead of ~ -signs, if used in my explanations, type an space/blank.
* This signs are RegEx Metachars => [\^$.|?*+(){} <= and have to
be escaped by an leading \ backslash if you want to match this sign itself and suppress their special meaning.
DIY:
To find your own solution you have to virtual (in mind) split your file names/strings into parts
following the rules of the regular expression syntax, see the help file coming with your application.
(Please note that there are several flavors of RE engines and also different implementations into apps
and even different ways of doing or thinking, so your experiences may differ from my explanation)
Once you have split your string into parts you can decide which to use into replacement by grouping the pattern
into (...) parenthesis to which you can refer by using "\1" or "$1" signs later, or which to drop and which to modify.
See the XYplorer Wiki for more about RegEx => http://88.191.26.34/XYwiki/index.php/Re ... xpressions
More Help
* Online tester:
- http://rereplace.com/
- http://www.regextester.com/
- http://www.regexlib.com/RETester.aspx
* Online help:
- http://www.regular-expressions.info
- http://www.regexlib.com/
- http://www.regexlib.com/CheatSheet.aspx
Do you use XYplorer to rename?
FROM:
A Clockwork Orange - Anthony Burgess.epub
TO:
Anthony Burgess - A Clockwork Orange.epub
Match all till an dash followed by an space ==> "(.+) - "
Match the rest ==> "(.+)"
till an dot and the extension ===> "\.(.{3,4})"
Swap the order ==> "$2 - $1.$3"
Use XYplorer RegExReplace...
(.+) - (.+)\.(.{3,4}) > $2 - $1.$3
----------
Also is there a way to remove anything between two brakets? Like:
FROM:
Zoey Dean - [A-List 06] - Some Like It Hot.epub
TO:
Zoey Dean - Some Like It Hot.epub
Match all till an [ ==> "(.+)\["
Match all till an ] space dash space ==> ".+] - "
Match all to the end ==> "(.+)"
New output ==> "$1 $2"
Use XYplorer RegExReplace...
(.+)\[.+] - (.+) > $1$2
EDIT:
General notes/ Disclaimer
Hope this helps ?

If yes, please help two others too. And consider an donation to the tools autor.
Please note:
Always don't trust me! Test with test files first!
* Usually i do a few tests on this issue only!
* So please test my solution with some test files first before you destroy your data.
RegEx is an pattern matching solution, so all your files have to fit into the same pattern.
If they not, you have to separate them and run some more actions against them.
BTW: the terms "RegEx" or "RegExp" or "RE" are the abbreviation of this pattern matching concept (Regular Expressions)
and also used to call the regex pattern thyself.
My solution depends on the quality of your examples.
* It's always an good idea to provide all possibilities of file name pattern in question.
* That would give the supporter an change to do it right

* If your real file names doesn't fit into your example pattern my solution may fail.
So provide us always real live examples of your file names. (Of course you can sophisticate your secrets)
Hint:
* Don't use this ' ' or " " -quotes from my explanation. They are only for clarification.
* '?' means non-greedy: stop at first match, instead of last possible.
* This (...) parenthesis are used to "group" what is found by this RegEx inside the ( )'s
to reuse this capture later as replacement by using \1 or $1 (depending by the regex engine).
* Instead of ~ -signs, if used in my explanations, type an space/blank.
* This signs are RegEx Metachars => [\^$.|?*+(){} <= and have to
be escaped by an leading \ backslash if you want to match this sign itself and suppress their special meaning.
DIY:
To find your own solution you have to virtual (in mind) split your file names/strings into parts
following the rules of the regular expression syntax, see the help file coming with your application.
(Please note that there are several flavors of RE engines and also different implementations into apps
and even different ways of doing or thinking, so your experiences may differ from my explanation)
Once you have split your string into parts you can decide which to use into replacement by grouping the pattern
into (...) parenthesis to which you can refer by using "\1" or "$1" signs later, or which to drop and which to modify.
See the XYplorer Wiki for more about RegEx => http://88.191.26.34/XYwiki/index.php/Re ... xpressions
More Help
* Online tester:
- http://rereplace.com/
- http://www.regextester.com/
- http://www.regexlib.com/RETester.aspx
* Online help:
- http://www.regular-expressions.info
- http://www.regexlib.com/
- http://www.regexlib.com/CheatSheet.aspx
Last edited by Stefan on 04 Aug 2010 08:42, edited 1 time in total.
Re: Regex help for renaming files
Thanks so much for all your help with this. I am going to mess with it more tonight after work.
worked like a charm along with all the other sugestions. Thanks again. Im just getting my feet wet with XY and RegEx.
Code: Select all
(.+) - (.+)\.(.{3,4}) > $2 - $1.$3
Re: Regex help for renaming files
codyd,
You and me both, I'm also just dipping in an out of Regex. It's fun but the learning curve is steep
I also wanted to add my thanks to Stefan, vegard and Don . . . just reading through these simple examples or regex find and replace patterns has taught me more than I've learned in quite a while
Stefans 'breakdown' of the request and a step by step process for reaching the answer has been worth a thousand words to me
so, thank you
Biggy
You and me both, I'm also just dipping in an out of Regex. It's fun but the learning curve is steep

I also wanted to add my thanks to Stefan, vegard and Don . . . just reading through these simple examples or regex find and replace patterns has taught me more than I've learned in quite a while
Stefans 'breakdown' of the request and a step by step process for reaching the answer has been worth a thousand words to me
so, thank you

Biggy
-
- Posts: 2341
- Joined: 04 Jan 2010 14:27
- Location: Pasárgada (eu vou!)
Re: Regex help for renaming files
For the sake of potential newcomers who may browse this, I'd like to add that they can achieve a simple result by using the aforementioned regex codes in scripts like this:
This may sound obvious to script talented, but can be a challenge to others (like me) who do not speak such language.
Just in hopes to help.
Code: Select all
//will swap "this" with "THAT" in a file like: this - THAT.ext; output: THAT - this.ext
rename r, "(.+) - (.+)\.(.{3,4}) > $2 - $1.$3";
Code: Select all
//"Zoey Dean - [A-List 06] - Some Like It Hot.epub" to "Zoey Dean - Some Like It Hot.epub"
rename r, "(.+)\[.+] - (.+) > $1$2";
Just in hopes to help.
New User's Ref. Guide and Quick Setup Guide can help a bit! Check XYplorer Resources Index for many useful links!
Want a new XYperience? XY MOD - surfYnXoard
-coz' the aim of computing is to free us to LIVE...
Want a new XYperience? XY MOD - surfYnXoard
-coz' the aim of computing is to free us to LIVE...
Re: Regex help for renaming files
SkyFrontier,
Once again, many thanks. As you say, I'm sure it's quite simple once you know how!
These little examples are just like someone turning on a light in a dark room . . . all of a sudden, I just go "Aah, now I get it!". I've spent hours looking over tutorials with regex but it's hard to overstimate just how useful a few simple examples can be
With these above, I'm already expanding them to do all kinds of useful things!
and it gives me the confidence to go on and learn more
Biggy
Once again, many thanks. As you say, I'm sure it's quite simple once you know how!
These little examples are just like someone turning on a light in a dark room . . . all of a sudden, I just go "Aah, now I get it!". I've spent hours looking over tutorials with regex but it's hard to overstimate just how useful a few simple examples can be
With these above, I'm already expanding them to do all kinds of useful things!

Biggy
Re: Regex help for renaming files
Thanks for the feedback and the interest
I have update my post above with my default disclaimer to provide more infos for interested users. (native english speakers may spell- and syntax check them
)
If one have more questions about XY related RegEx... just ask.

I have update my post above with my default disclaimer to provide more infos for interested users. (native english speakers may spell- and syntax check them

If one have more questions about XY related RegEx... just ask.
I assume XY's RegEx is VB 6? (Plus Learning Link and Utility
I assume the RegEx flavor is just Visual Basic 6? Seems to be...?
(Anyone interested might want to check out https://www.regexbuddy.com/ <- fantastic utility and teaching tool; there's a free trial period, and a three month return period. If you use them, this will be useful for years. If you program, even more so. But great for so many purposes. Also, this is a fantastic page for learning: http://www.regular-expressions.info <- Related to above site, but is simply a great grounding in RegExes. He also has the very good, free, EditPad Lite - very strong in RegEx support - and an ultra-powerful Windows GREP tool and other things.)
(Anyone interested might want to check out https://www.regexbuddy.com/ <- fantastic utility and teaching tool; there's a free trial period, and a three month return period. If you use them, this will be useful for years. If you program, even more so. But great for so many purposes. Also, this is a fantastic page for learning: http://www.regular-expressions.info <- Related to above site, but is simply a great grounding in RegExes. He also has the very good, free, EditPad Lite - very strong in RegEx support - and an ultra-powerful Windows GREP tool and other things.)
Re: Regex help for renaming files
@Dustydog:
I can't find a trail. Looks like you have to buy without a try.
The regex page is very nice, and enough. I made a small xyscript for testing regexmatches/regexreplace. That's enough I think. No need to spend money for something you use only once per month.
I can't find a trail. Looks like you have to buy without a try.

The regex page is very nice, and enough. I made a small xyscript for testing regexmatches/regexreplace. That's enough I think. No need to spend money for something you use only once per month.

In special cases you may contact me on Discord: http://bit.ly/dsDisco
Re: Regex help for renaming files
Yes, XY uses the VB5 regex engine.
Psst, there's something in the user functions exchange that allows you to use some advanced regexp, at least within the context of scripting.

Icon Names | Onyx | Undocumented Commands | xypcre
[ this user is asleep ]
[ this user is asleep ]
Here's the Link to that RegEx Buddy Trial
Here's at least one link to the trial. It's for a week, and I knew I'd used the trial in the past so it was somewhere or other. The site where I found the link also has some terrific regex training information on it (some advanced topics are incredibly clear), so I left the link as is:ds1508 wrote:@Dustydog:
I can't find a trail. Looks like you have to buy without a try.![]()
The regex page is very nice, and enough. I made a small xyscript for testing regexmatches/regexreplace. That's enough I think. No need to spend money for something you use only once per month.
http://www.rexegg.com/regexbuddy-trial.html
That three month guarantee does provide a nice, long trial if you just want it for learning, but at this point, I'll never give it up. Great, great tool. Didn't realize how much I'd love and use it. Also has a good built-in Windows grep that launches from within it, complete with preview.
I've managed some insanely complicated regexes that match some of my standard file formatting, and the usual differences, that I'd never have been able to manage without it. With those in hand (with the groups labeled especially), I can reuse it for a ton of file normalization just by tweaking (often only) the replacement string. It will also automatically convert from a regex flavor that supports named groups to VB which doesn't - not that necessary, but handy. It also helps you keep track of your regex library complete with sample text.
I'd really recommend giving their EditPad lite (free, no strings) a try. The only thing that the pro version has (other than some more advanced code editing and word processing abilities) that I feel it really lacks is the ability to move blocks, but I use that infrequently enough that Notepad++ certainly suffices. It's a great pad in which to mess with regexes. Plus it's just a nice tabbed notepad with a lot of features.
You really won't have any idea how good that software is unless you give it a trial with some task at hand that takes something complicated. It...demystifies brilliantly while providing a nice sandbox. XY giving us quick access to a clipboard full of file and directory names is great for supplying test text - and I don't even need to use a batch file
