REGEX: Change the name in reverse order
-
The names are upside down, I want to change them in ascending order. They are from 152 to 162 in the name on the hdd, but actually it was a mistake, they should have the files in the reverse order. So after replace I will have 162 instead of 151 and so on.
Example:
masini-unelte-emil-botez-carte-p1_compress_Page_151.jpg masini-unelte-emil-botez-carte-p1_compress_Page_152.jpg masini-unelte-emil-botez-carte-p1_compress_Page_153.jpg masini-unelte-emil-botez-carte-p1_compress_Page_154.jpg masini-unelte-emil-botez-carte-p1_compress_Page_155.jpg masini-unelte-emil-botez-carte-p1_compress_Page_156.jpg masini-unelte-emil-botez-carte-p1_compress_Page_157.jpg masini-unelte-emil-botez-carte-p1_compress_Page_158.jpg masini-unelte-emil-botez-carte-p1_compress_Page_159.jpg masini-unelte-emil-botez-carte-p1_compress_Page_160.jpg masini-unelte-emil-botez-carte-p1_compress_Page_161.jpg masini-unelte-emil-botez-carte-p1_compress_Page_162.jpgSo the basis would be this.
FIND:masini-unelte-emil-botez-carte-p1_compress_Page_\d+(.*?)\.jpgfrom here I have to select all the names and change the part of \d+ i.e. the numbers, so that I can reverse them.
Output must be:
(162 ↔ 151; 161 ↔ 152; 160 ↔ 153...)I try this, but not very good:
FInd:
_Page_(15([1-9])|16([0-2]))\.jpg
Replace:_Page_\2\3\1.jpg -
So what is wrong with the sorting options from Edit->Line Operations? What are you trying to achieve that can’t be solved by them?
-
I have over 2000 txt files, and I want to use regex. I already have a very good Python code. But I want regex. Maybe @guy038 knows a regex solution.
import os import re FOLDER = r"g:\Colectia EMINESCIANA\tst" PATTERN = re.compile(r"^(.*_Page_)(\d+)(\.jpg)$", re.IGNORECASE) fisiere = [] for nume in os.listdir(FOLDER): m = PATTERN.match(nume) if m and os.path.isfile(os.path.join(FOLDER, nume)): fisiere.append((int(m.group(2)), nume, m.group(1), m.group(2), m.group(3))) if not fisiere: raise SystemExit("Nu s-a găsit niciun fișier care să corespundă tiparului.") fisiere.sort(key=lambda x: x[0]) numere = [f[3] for f in fisiere] perechi = list(zip([f[1] for f in fisiere], numere[::-1])) print(f"Se inversează {len(perechi)} fișiere din {FOLDER}\n") for (vechi, nou_num), (_, _, prefix, _, ext) in zip(perechi, fisiere): print(f" {vechi} -> {prefix}{nou_num}{ext}") if input("\nConfirmi redenumirea? (d/n): ").strip().lower() != "d": raise SystemExit("Anulat.") temporare = [] for i, (vechi, _) in enumerate(perechi): tmp = f"__tmp_{i}__.jpg" os.rename(os.path.join(FOLDER, vechi), os.path.join(FOLDER, tmp)) temporare.append(tmp) for tmp, ((_, nou_num), (_, _, prefix, _, ext)) in zip(temporare, zip(perechi, fisiere)): os.rename(os.path.join(FOLDER, tmp), os.path.join(FOLDER, f"{prefix}{nou_num}{ext}")) print("\nRedenumire finalizată cu succes!") -
I already have a very good Python code. But I want regex.
The Python code (or other programming language) is the right solution.
Regex doesn’t work the way you want it to, and do if you’ve got more than one swap to do per file, regex will not solve your problem.
Hello! It looks like you're interested in this conversation, but you don't have an account yet.
Getting fed up of having to scroll through the same posts each visit? When you register for an account, you'll always come back to exactly where you were before, and choose to be notified of new replies (either via email, or push notification). You'll also be able to save bookmarks and upvote posts to show your appreciation to other community members.
With your input, this post could be even better 💗
Register Login