Community
    • Login

    REGEX: Change the name in reverse order

    Scheduled Pinned Locked Moved Help wanted · · · – – – · · ·
    4 Posts 3 Posters 87 Views 2 Watching
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • Neculai I. FantanaruN Offline
      Neculai I. Fantanaru
      last edited by Neculai I. Fantanaru

      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.jpg
      

      So the basis would be this.
      FIND: masini-unelte-emil-botez-carte-p1_compress_Page_\d+(.*?)\.jpg

      from 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

      gerdb42G 1 Reply Last reply Reply Quote 0
      • gerdb42G Offline
        gerdb42 @Neculai I. Fantanaru
        last edited by

        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?

        Neculai I. FantanaruN 1 Reply Last reply Reply Quote 0
        • Neculai I. FantanaruN Offline
          Neculai I. Fantanaru @gerdb42
          last edited by Neculai I. Fantanaru

          @gerdb42

          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!")
          
          PeterJonesP 1 Reply Last reply Reply Quote 0
          • PeterJonesP Offline
            PeterJones @Neculai I. Fantanaru
            last edited by PeterJones

            @Neculai-I.-Fantanaru said:

            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.

            1 Reply Last reply Reply Quote 0

            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
            • First post
              Last post
            The Community of users of the Notepad++ text editor.
            Powered by NodeBB | Contributors