RegEx: how do I get the length of match group?

If you need help on how to use the program
Post Reply
regex_student
Posts: 4
Joined: 2025-07-28 23:39:41

RegEx: how do I get the length of match group?

Post by regex_student »

I have files with variable length names which I would like to rename them to a fixed length
from

Code: Select all

1.txt
23.txt
492.txt
to

Code: Select all

00001.txt
00023.txt
00492.txt
Ant Renamer Regular expressions renaming has a link to TRegExpr manual that menthions MatchLen property for Groups, but I couldn't find how to get that info and use it in the New name field for Regular expressions renaming

any help and hint on how to do this would be appreciated
antp
Site Admin
Posts: 9742
Joined: 2002-05-30 10:13:07
Location: Brussels
Contact:

Re: RegEx: how do I get the length of match group?

Post by antp »

Hi,
Unfortunately you cannot access that info from the program, that's something for the code itself.
I'm not sure it is so simple to do padding with regular expression (you can maybe find some more info on internet by searching that, but I recall that it was not so simple in one pass).
However what you can do is add a fixed number of zeros in front (e.g. 4) and then using regular expression limit to keep only last x characters.
regex_student
Posts: 4
Joined: 2025-07-28 23:39:41

Re: RegEx: how do I get the length of match group?

Post by regex_student »

Hi,

since the info are available in the code, is it possible to add new list (similar to multi-string replace) to the Regular expression renaming where user can add modifications to the captured group? in this case, I would add action to make the capture group 1 to have at least 5 chars and left pad char '0' when the group is less than 5 chars
antp
Site Admin
Posts: 9742
Joined: 2002-05-30 10:13:07
Location: Brussels
Contact:

Re: RegEx: how do I get the length of match group?

Post by antp »

It would be possible, sure, but a lot of things could be done on groups, in this case you want to make padding but there is an infinite number of possible actions :D
I could try to make a list of useful actions, but all that would be a major feature to add, and my to-do list is already so much bigger than my available time for maintaining the software, so it is unlikely that I would have time for that unfortunately.
In the meantime, in this case there is a workaround requiring two different actions (inserting zeroes and then limiting the number of characters).
regex_student
Posts: 4
Joined: 2025-07-28 23:39:41

Re: RegEx: how do I get the length of match group?

Post by regex_student »

I have been thinking about how to do the two step actions you had suggested, just haven't figured out the exact expression to limit or trim the number of digits due the actual filenames have various random length strings before and after the numbers

something like

Code: Select all

wolf_tdi3.blahtlba.1.plowya.txt
al34bast.23.lote.est1883.txt
kingston5400.492.ddr5-cuughy.txt
regex_student
Posts: 4
Joined: 2025-07-28 23:39:41

Re: RegEx: how do I get the length of match group?

Post by regex_student »

antp wrote: 2025-07-30 06:51:57 It would be possible, sure, but a lot of things could be done on groups, in this case you want to make padding but there is an infinite number of possible actions :D
I could try to make a list of useful actions, but all that would be a major feature to add, and my to-do list is already so much bigger than my available time for maintaining the software, so it is unlikely that I would have time for that unfortunately.
In the meantime, in this case there is a workaround requiring two different actions (inserting zeroes and then limiting the number of characters).
understood. would be good if the new feature would allow me to do something similar to C string format. in this case, printf like %05u on the captured group
antp
Site Admin
Posts: 9742
Joined: 2002-05-30 10:13:07
Location: Brussels
Contact:

Re: RegEx: how do I get the length of match group?

Post by antp »

That could be an idea, indeed.
For the second step to remove extra zeroes, if you add always 4 zeroes, you can then match (.+)\.0*(\d{4})\.(.+) and then keep only that group to discard the useless zeroes: \1\2\3 as final name.
To be tested, I assume it would work but I didn't try :D
Inserting the zeroes for the first step could use a similar expression.
Post Reply