Change/replace digits and keep intact parts

A swapping-ground for Regular Expression syntax

Change/replace digits and keep intact parts

Postby metasse » Fri Oct 30, 2009 6:50 pm

Hi all,

i am trying to understand reg expressions but can't find a solution to my problem,
i need to rename files like this:
videofile1.avi

to this:

videofile.CD1.avi

i found how to change 1.avi to .CD1.avi but
I can't find a way to keep "videofile".

i'm sure it must be simple for expert but as a regexp noob i am struggling...

Thanks in advance.
Last edited by metasse on Mon Nov 23, 2009 12:04 pm, edited 1 time in total.
metasse
 
Posts: 4
Joined: Fri Oct 30, 2009 6:43 pm

Re: Can't find the right expression...

Postby Stefan » Fri Nov 20, 2009 11:57 pm

metasse wrote: i need to rename files like this:
videofile1.avi
to this:
videofile.CD1.avi

Hi,
One is not enough examples to give an valid answer,
but i do a guess for
videofile1.avi
videofile2.avi
videofile3.avi
videofile15.avi
videofile0016.avi


RegEx(1)
Match: (.+)(\d+)$
Replace: \1.CD\2

Explanation:

Match
.+ ==> find one or more of any char, till
\d+ ==> find one or more digits
( ) ==> group the matched content for back reference
$ ==> followed by the end of the string/filename (that means: take only digits at the end into account)

Replace
\1 ==> content what is matched in group 1
. ==> an dot
CD ==> 'CD'
\2 ==> the found digits
Stefan
 
Posts: 736
Joined: Fri Mar 11, 2005 7:46 pm
Location: Germany, EU

Re: Can't find the right expression...

Postby metasse » Sun Nov 22, 2009 10:30 am

Thank you so much Sir, I had given up on this one and you just got me the perfect solution.
I have just tested it and it's working very well! :D
metasse
 
Posts: 4
Joined: Fri Oct 30, 2009 6:43 pm

Re: Can't find the right expression...

Postby metasse » Sun Nov 22, 2009 11:11 am

mmm.... sorry for being back with a new question, but another situation has arised... :(
Some of the files I'd like to rename are also named " videofile1.en.srt" or "videofile1.fr.srt"
Is there anyway to also rename those with a regexpression?

i'd like to rename them into "videofile.CD1.en.srt" and "videofile.CD1.fr.srt"

For your information, the digits are only 1 or 2, never anything else.
Also I only have "en" or "eng" or "fr" or "fra" in the srt files.
The only thing that can change to anything are the "videofile" characters.

Thanks again for your assistance.
metasse
 
Posts: 4
Joined: Fri Oct 30, 2009 6:43 pm

Re: Can't find the right expression...

Postby Stefan » Sun Nov 22, 2009 1:51 pm

Hi metasse,

that's exactly what i had meant with "one is not enough example" :D
To give an valid answer i need examples with all possible variations of you file names.

New challenge:

FROM
"videofile1.avi"
"videofile12.avi"
"videofile1.en.srt"
"videofile99.eng.srt"
"videofile1.fr.srt"
"videofile12.fr.srt"
"videofile12.fra.srt"

TO
"videofile.CD1.avi"
"videofile.CD2.avi"
"videofile.CD1.en.srt"
"videofile.CD99.en.srt"
"videofile.CD1.fr.srt"
"videofile.CD12.fr.srt"
"videofile.CD12.fra.srt"

Questions:
* is 'srt' an extension like 'avi' ? (i use Yes as your answer)
* are there any digits in the word 'videofile' before those last one or two ? (i use No as your answer)


I will give you an short lesson so you can see the trick.
Note that there are many ways to get an result, depending what your file name contains, the RegEx engine of the tool and on the habit of the author :lol:
See this older threads for an RegEx overview:
=> http://www.bulkrenameutility.co.uk/forum/viewtopic.php?f=3&t=5
=> http://www.bulkrenameutility.co.uk/forum/viewtopic.php?f=3&t=27

* first we match one or more of any char ==> .+ and group this ==> (.+) but don't be greedy ==> (.*?)
* then we search for one digit ==> \d followed by one or no digit ==> \d* and group this too ====> (\d\d*)

* then, for some file names there follows the the end ==> $, but for others there are additional chars like '.en' or '.eng', resp. '.fr' or '.fra'
So i drop this "match the end by $" behaviour to be able to cover all possibilities with one RegEx match:
Match-or-not an dot and two or three chars (for simplify of the RegEx i search for one-or-more chars ) and group this ==> ([\.\w+]*)

So i would use
RegEx(1)
Match: (.+?)(\d\d*)([\.\w+]*)
Repla: \1.CD\2\3
[ ] Include Extension un-checked

Explanation:
(.+?) ==> match one-or-more of any sign, but don't be greedy and match to much,
to not match first of the digits too (maybe (\w+) would be clearer to understand)
(\d\d*) ==> match one digit followed by one-or-no digit
([\.\w+]*) ==> match one dot or one-or-more any chars

---
Thinking some more this third match could be simplified to (.*) too
(.*) ==> match non-or-more of any sign (that's there is an '.en' or like that or not)
Match: (.+?)(\d\d*)(.*)
But each simplifying is an risk to don't really match what you want too.
So just try it (that's anyway the key to mastering RegEx: trial-and-error, reading the rules and test, test, test,...)
---

\1 ==> will hold and give back "videofile"
.CD => will put in just this: '.CD'
\2 ==> will hold and give back the digit(s)
\3 ==> will hold nothing or give back ".en" or ".fra" or like that, without the ".srt"-part

Image


--

Or this will work here too:
RegEx(1)
Match: (.+?)(\d+.*)
Repla: \1.CD\2
[ ] Include Extension un-checked

Explanation:
(.+?) ===> match one-or-more any signs non-greedy
(\d+.*) => match one-or-more digit(s) followed by no-or-more of any signs (that's dot or chars here)

As i said: there are many ways. Depending on your filenames one or all may fail to work.
Try and pick the right one for you. Or come up with an totally other solution :wink:


HTH :lol:
If yes: please help two others too.


And you could please modify the non-saying Subject of this thread
from "Can't find the right expression..."
to smtg like "Match and move digits" or "Reorder digits and add text" or like that...
Stefan
 
Posts: 736
Joined: Fri Mar 11, 2005 7:46 pm
Location: Germany, EU

Re: Can't find the right expression...

Postby metasse » Mon Nov 23, 2009 12:03 pm

This is Beautiful, you've just saved me such a hassle, thank u thank u :D
You made the right assumptions regarding the extensions as well as the digits.

To be honest i am having trouble understanding the logic behind the expressions, I know for some it's just plain easy but my brain does not seem to open up to this :idea: . Still I will try to understand it as it seems extremely useful!

You are right i should change the subject line, that is if i find the option somewhere...

Thanks again!
metasse
 
Posts: 4
Joined: Fri Oct 30, 2009 6:43 pm

Re: Change/replace digits and keep intact parts

Postby ThanhLoan » Wed Dec 16, 2009 2:17 pm

Hi Stefan,
I read this post I and think I can ask you for help :
I would like to extract the first uppercase letter of the last words in a filename and paste them to the end of the filename.
Exp : old filename : SunFly116_12-The Beatles- I Want To Hold Your Hand.mkv
new filename : SunFly116_12-The Beatles- I Want To Hold Your Hand-IWTHYH.mkv

Thank you in advance for your help.
ThanhLoan
 
Posts: 7
Joined: Fri Dec 11, 2009 8:22 pm

Re: Change/replace digits and keep intact parts

Postby zunixaani » Thu Feb 26, 2015 11:43 am

That helps massively - thanks so much :)


__________________
Lily lara
zunixaani
 
Posts: 1
Joined: Thu Feb 26, 2015 11:37 am


Return to Regular Expressions


cron