0

I'm pretty new to RegEx, but have been interested in it since I began using Bulk Rename Utility. I've used the other functions in the program, but seeing that RegEx is more complex, I'd like to see if it could solve my problem better.

For example, I have videos dated like this;

2020-7-3; blah.mp4
2022-08-03_blah.mp4
2013-04-03-20-34-08; blah.mp4

But I want them to appear like this;

2020(07-03)133452_blah.mp4
2022(08-03)143136_blah.mp4
2013(04-03)203408_blah.mp4

Where the first number is the year, the numbers in parenthesis are month/day, and the last digits are the timestamp it was published. I can't seem to use the other formulas/settings (i.e. "Date Modified", etc.) because the original date for a lot of files have been lost in the re-(re-re?)encoding process, so having the OG date in the name is very important.

Is there a way to take the numbers in the name and rehash the symbols so they display like the sample I gave? Is there any way to do this all in one expression? Thanks in advance :)

I haven't tried much yet; I'm pretty new to RegEx… I'd be eternally grateful to anyone who could help point me in the right direction to learn it through this example.

  • I'm not sure this question is suitable for this site. Either way, could you describe your condition a bit better? How does the regex replacement mechanism work? – code Feb 21 '23 at 05:01

1 Answers1

0

the regex construct you are looking for are named groups ((?'groupname'abc)). Later when replacing you can use the named group to specify the match ($groupname).

For readability I suggest two steps. First add a zero before single numbers (like 7 and 3 in your original example) and then reformat the filenames to the wanted shape.

Adding the Zero: I think this is the hard part. We need multiple different regex constructs. Lookaheads (?=foo), lookbehinds (?<=bar), conditional (?(foobar)) and named groups (?'groupname').

  • Lookahead and Lookbehind are zero-length assertions (they are not matched directly) are used to assert that the match is followed or proceeded by an other match. For example the expression (?<=bar)\d translates to match a number if it is preceeded by the word bar.
  • conditionals (?:(foo) foo| bar) will match the part before | if the ?:(foo) expression matches.
  • named groups will give a match a name which can be used in substitution. E.g. (?'name') can be used in the substitution process using $name.

With this in mind the expression I found that accomplishes the replacement is this one (/(?<=-)(?(?=\d{2})|(?'oneLetter'\d))(?=\D)/gm).

For the reformating part I found another expresion which can then be used to get to the filename shape you want. here. The expresion I use to match the filenames is (?'year'\d{4})-(?'month'\d{1,2})-(?'day'\d{1,2})(.*.mp4). \d matches a number and the number in the curly braces specifies how many number you want to match. The .* at the end matches everything until the end of the line. Then you can use $year\($month-$day\).mp4 to use the named groups to get the filename shape you specified.

I hope this example helps!

schlin
  • 117
  • 5
  • This is awesome! The regex101 page really helped! Thank you so much :) Now, in the case of the 2020-7-3 name, how would I add zeros to the single-digit dates? (so it displays as 2020(07-03)?) – MintKakapo Feb 21 '23 at 18:54
  • Hi, i missed that part in the originial question. I think the easiest way to get to that form is in two steps. You could do it in one but the expression will get pretty messy. I updated the original answer and added the missing step – schlin Feb 22 '23 at 07:16