Friday, April 26, 2024
 Popular · Latest · Hot · Upcoming
7
rated 0 times [  7] [ 0]  / answers: 1 / hits: 2283  / 3 Years ago, mon, october 4, 2021, 5:30:24

I've been using GPRename to batch-rename files; this is rather more efficient than individually correcting each file, but still seems to be less efficient than it might be, primarily because either I don't understand the regex syntax used, or because the regex implementation is incomplete1



Given a list of files of the following syntax:



(01) - title of file1.avi
(02) - title of file2.avi
(03) - title of file3.avi


I attempted to use the 'replace' (with the regex option selected, the case-sensitive option deselected):



((d{2}))


The preview then shows (given that I've specified no 'replace with' option as yet):



title of file1.avi
title of file2.avi
title of file3.avi


Which is great, clearly the regex is identifying the correct group (the (01)). Now, what I was hoping to do (using the JavaScript syntax) in the 'replace with' option is use:



$1


(I also tried using '$1', 1 and '1')



This was just to check that I could access the matched group, and it seems I can't, the matched group is, as I suppose might be expected, replaced with the literal replacement string.



So, my question: is it possible to match a particular group of characters, in this case the numbers within the brackets, and then insert those into the replacement string? Therefore:



(01) title of file1.avi
(02) title of file2.avi
(03) title of file3.avi


Becomes:



01 title of file1.avi
02 title of file2.avi
03 title of file3.avi






  1. I absolutely suspect the former, personally.


More From » regex

 Answers
1

Unfortunately, it appears you cannot; and that it's an incomplete regex implementation at fault.



This line at the end of all the GPRename locale translations seems to make that obvious:




msgid "Backreferences with Regular expression cannot be use
as $1 is considered a Perl variable."


Since you are familiar with Perl regular expressions, may I recommend you use the excellent rename command-line utility instead?




  • Always use the -n switch first to get a preview.

  • There's a typo in your regex - it should be ((d{2})), with the grouping inside the escaped parentheses.

  • Example:



    $ rename -n 's/((d{2}))/$1/' *.avi
    (01) title of file1.avi renamed as 01 title of file1.avi
    (02) title of file2.avi renamed as 02 title of file2.avi
    (03) title of file3.avi renamed as 03 title of file3.avi

  • Call rename with the -v (verbose) switch and drop the -n to actually perform the renaming.


[#35768] Tuesday, October 5, 2021, 3 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
sweetrifiabl

Total Points: 422
Total Questions: 94
Total Answers: 120

Location: Bonaire
Member since Sat, Sep 24, 2022
2 Years ago
sweetrifiabl questions
Mon, Apr 4, 22, 16:02, 2 Years ago
Sat, Jul 17, 21, 01:40, 3 Years ago
Thu, Nov 18, 21, 17:36, 2 Years ago
Mon, Jan 17, 22, 19:02, 2 Years ago
;