Replace function combined with regex

Advanced Renamer forum
#1 : 14/06-25 15:49
GSComputer
Posts: 39
I think it is a Delta Foxtrott question
sure I apreciate any help by all

I would like it to do with regex
I have files sometimes I get them with various names but the result should be always same

news525.pdf 5 is the month; 25 year
news52025 5 is the month; 2025 year
news0525.pdf 05 is the month; 25 year
news052025.pdf 05 is the month; 2025 year

$1 means month $2 means Year

The result should always be: news 2025 05
My idea is if news is the following:
with three digits rename to 20$2 0$1
with four digits rename to 20$2 $1
with five digits rename to $2 0$1
with six digits rename it to $2 $1

I have a solution with four replace for each case, but I think it should work with one Regex combined IF Else function
Maybe the lookahead function 3 4 5 or 6 digits could help.

I need a trigger to solve it

Looking forward
GS

edited: 14/06-25 15:50
#2 : 14/06-25 21:13
Delta Foxtrot
Posts: 496
Reply to #1:

Hey G,

Dude, you don't ask much! :) But perhaps your confidence in my abilities are a bit beyond reality.

I don't know why you'd want to use a big complicated expression when four comprehensible little ones would do, but... I'm not judging! Here's the closest I got in an hour of fiddling:

[BTW, since there are no spaces in the names I used (?x) at the start, which allows adding spaces that don't effect the expression ("whitespace" mode), just to make it *somewhat* clearer]

Replace: (?x) (?=\d{6}$) (\d\d) (\d{4}) | (?=\d{5}$) (\d) (\d{4}) | (?=\d{4}$) (\d\d) (\d\d) | (?=\d{3}$) (\d) (\d\d)

Replace with: $2$4$6$8 $1$3$5$7

Screenshot: https://drive.google.com/file/d/1iwUeSk2R5hDj2sQ EKqFgf-rm_Cv_zawq/view?usp=sharing

That gets you everything in the right place, just not filled out with the missing "20"s on the years and "0"s on the months. I've never really used conditionals (if...then...else) in regex, it's always been easier for me just to go to code of some sort; I was unable to get further than this. Maybe an *actual* regex expert can get you there, or maybe some industrious little AI lurking on the net somewhere.

With my limited knowledge you'd still need two more replaces to get where you want to go. As I mentioned above, I'd rather use four simple replaces, or a List replace with four expressions, than three replaces with one being the s***show I used above.

Cheers my friend,
DF

edited: 14/06-25 21:16
#3 : 15/06-25 03:26
GSComputer
Posts: 39
Reply to #2:
Hallo DF,

maybe you remember what I use the aren file for:
I get every Day a bunch of newspapers, illustrated papers articles, brochures and so on.
In former times I collected it in my office with no real order.

In the german language persons like this are called "Hunter and Collector". My wife says I have the "Hoarding Syndrom"
But since the time I collect that all in *.pdf, my office looks proper cleaned up and tidy. -;)

How I do it:
All incoming Stuff is in Paperdownload

1. I replace Monthnames to 2 digits
2. I trim that all looks like <namemaybesomeotherblabla><DateData
3. I rename each file name to <newname> <date> Date cound be today or year month or year weeknr or year number

Examples "Dayly Reports 01.12.2025" "Monthly Reports 2025 08" "Weekly Reports 2025 34" or Report 2025 436

Sure with 10 files it would not be a big deal.
Oops the word "deal" is reserved by an other important person. His name is TWGDM (The world's greatest deal maker) ;-) ---> A little dig at America and MAGA
Meanwhile the are-file has over 200 methods mostly Replace.

The complicated expression I described would minimize my aren-file.
The example in #1 is one of the things I would like to optimize

So far so good, my solution works, not perfect but it works.
Problems with regex because it works sometimes also on other methods; I never understood Occurence all 1. 2. ... and afew other things

My aim is to name the files cleanly and clearly always with the same logic

More PRIVATE
I do not overestimate your knowledge, you always have solutuions and tips and tricks how to solve my "renaming problems".
your proposals in #2 give me again new ideas included to use AI.
I'll stay tuned.

After being ill for a longer time, I now have more time to think, maybe solve, problems which are not real problems.
But I hope engaging in logical things like mathematics, regex, scripts s. o. keeps my 75-year old brain alive. ;-)

By the way, while I am writing this, I'm drinking a glas red wine Rioja and lsten to Roy Buchanan - When A guitar plays the blues.
https://www.youtube.com/watch?v=jOLbnrQIxlM

Cheers and have a good time my friend
Gerhard

edited: 15/06-25 03:59
#4 : 15/06-25 05:09
Delta Foxtrot
Posts: 496
Reply to #3:

... and now I'm listening to the Roy-meister play the blues! Although it's hard to concentrate on typing with all that going on in the background! :)

First Kim, a few threads back, saying how dumb we Yanks are for starting the week on the weekend, and now you gettin' your digs in... that's all right, us Ugly Americans can take it! :)

Anyway, I do remember your use case, though when you have time to read all those papers is a mystery to me, between all that drinking wine and listening to the blues! And btw, here we call people like you and me "hoarders", so wife was on target there.

OK, down to business... I don't know if it will help you, but here's a trick I use that works great for me when I have lots of methods effecting lots of different patterns of filenames (and it works on PowerGREP too, which is a more powerful program that can do filename and file content changes using scripts composed of regex):

First, instead of just starting in changing filenames, identify the different patterns you'll be working with and add a distinctive character at the start of each filename. You'll probably need one method for each filename type just to add the characters, but it's not really much work. For instance, I might use Alt+1 (light happy-face) to identify pattern 1; Alt+2 (dark happy-face) to identify pattern 2; etc. Then, identify which methods need to work on which file patterns and have them check for the appropriate special character before acting on each filename. It saves me a lot of mistakes that happen when a method inadvertently changes a file of the wrong pattern.

Well, that's all for now, got to get back to Roy and his magic telecaster...

Best,
DF
#5 : 15/06-25 06:02
Delta Foxtrot
Posts: 496
Reply to #4:

But I just had to write a script first. LOL, sorry Roy!

This does the same thing, but it doesn't matter what else is in the filename; it just looks for the first group of digits (3 to 6 characters in length), then transforms them as you requested. The rest of the filename remains unchanged. I also wrote one that used a SWITCH statement to do the same, but this may be a little clearer, since everybody and their dog understands IF...THEN...ELSE.

name1 = item.newBasename ;
name2 = "" ;
date1 = "" ;
dateLen = 0 ;

if ( /\d{3,6}/.test( name1 ) ) {
date1 = /\d{3,6}/.exec( name1 ).toString() ;
dateLen = date1.length ;
} else {
return name1 ;
}

if ( dateLen > 6 || dateLen < 3 ) {
return name1 ;
}

if ( dateLen == 6 ) {
name2 = name1.replace( /(\d\d)(\d{4})/, " $2 $1" ) ;
}
if ( dateLen == 5 ) {
name2 = name1.replace( /(\d)(\d{4})/, " $2 0$1" ) ;
}
if ( dateLen == 4 ) {
name2 = name1.replace( /(\d\d)(\d\d)/, " 20$2 $1" ) ;
}
if ( dateLen == 3 ) {
name2 = name1.replace( /(\d)(\d\d)/, " 20$2 0$1" ) ;
}
return name2 ;


Best,
DF