Community
    • Login

    REGEX: Change the name in reverse order

    Scheduled Pinned Locked Moved Help wanted · · · – – – · · ·
    11 Posts 4 Posters 371 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.
    • 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 Online
          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.

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

            @PeterJones

            Every problem hides a solution to another problem. So, I just discover some king of Incrementation with REGEX. Someone said you can’t get increment with regex, well, you can. Lucky me.

            So I have this:

            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
            

            I use this regex:

            FIND: _Page_(15([1-9])|16([0-2]))\.jpg
            REPLACE BY: \3\2\1

            Output:

            masini-unelte-emil-botez-carte-p1_compress1151
            masini-unelte-emil-botez-carte-p1_compress2152
            masini-unelte-emil-botez-carte-p1_compress3153
            masini-unelte-emil-botez-carte-p1_compress4154
            masini-unelte-emil-botez-carte-p1_compress5155
            masini-unelte-emil-botez-carte-p1_compress6156
            masini-unelte-emil-botez-carte-p1_compress7157
            masini-unelte-emil-botez-carte-p1_compress8158
            masini-unelte-emil-botez-carte-p1_compress9159
            masini-unelte-emil-botez-carte-p1_compress0160
            masini-unelte-emil-botez-carte-p1_compress1161
            masini-unelte-emil-botez-carte-p1_compress2162
            

            Before each number at the end, I got another number in order. But I don’t know why I couldn’t get 10 or 11, and it started all over again from 1 to 0.

            Then I tested many variants, and found the fabulous solution to Incrementation with Regex:

            FIND: _Page_(15([1-9])|16([0-2]))\.jpg
            REPLACE: \2(?{3}1)\3\1

            I get this: 1 to 12 each line, in order"

            masini-unelte-emil-botez-carte-p1_compress1151
            masini-unelte-emil-botez-carte-p1_compress2152
            masini-unelte-emil-botez-carte-p1_compress3153
            masini-unelte-emil-botez-carte-p1_compress4154
            masini-unelte-emil-botez-carte-p1_compress5155
            masini-unelte-emil-botez-carte-p1_compress6156
            masini-unelte-emil-botez-carte-p1_compress7157
            masini-unelte-emil-botez-carte-p1_compress8158
            masini-unelte-emil-botez-carte-p1_compress9159
            masini-unelte-emil-botez-carte-p1_compress10160
            masini-unelte-emil-botez-carte-p1_compress11161
            masini-unelte-emil-botez-carte-p1_compress12162
            
            PeterJonesP 1 Reply Last reply Reply Quote 0
            • PeterJonesP Online
              PeterJones @Neculai I. Fantanaru
              last edited by PeterJones

              @Neculai-I.-Fantanaru ,

              It’s not your originally-stated request (ie, it does not put those exact numbers in reverse order), and it won’t work if your number range changes.

              But if it works for what you really want (rather than what you asked for), that’s great for you.

              Someone said you can’t get increment with regex, well, you can

              Not generically, you can’t.

              If you have a limited set of numbers, it’s really easy to do, as you found, by crafting regex cumbersome regex, but those get less viable the wider range of numbers you want – just try going from 101 - 199, for example; or, worse, from 101-9999 – . Guy has shown much wider ranges than your effort, but it will never be generic, as someone will always need “one more digit” than your regex provided.

              (update: I also don’t see how that’s “increment”, in any meaningful interpretation of the word: it doesn’t just “add X” to the original number; it just copies the last digit to before the first)


              Also: Per my reading of your original question, where I assumed “in reverse order” meant 162 ↔ 151; 161 ↔ 152; 160 ↔ 153 (like you said), rather than some weird rearrangement of the digits withing a given number (151→1151, 152→2152, …) (like I now see that I think your python did: I hadn’t bothered reading your python before, because I assumed it did what you said you wanted), I would now propose a different solution to what I thought you were asking for, given that you seem to only care about 151-162, and no other numbers: FIND = Page_(?:(151)|(152)|(153)|(154)|(155)|(156)|(157)|(158)|(159)|(160)|(161)|(162)) REPLACE = Page_(?{1}162)(?{2}161)(?{3}160)(?{4}159)(?{5}158)(?{6}157)(?{7}156)(?{8}155)(?{9}154)(?{10}153)(?{11}152)(?{12}151) – but since that’s not generic, it’s 99.9999% of the time not what someone would would want, if they phrased the question the way you did in your title and your mapping. I now don’t think this is actually what you wanted, based on your python and your pseudo-increment; but if it is what you originally wanted, feel free to use it.

              1 Reply Last reply Reply Quote 2
              • guy038G Offline
                guy038
                last edited by

                Hello, @neculai-i.-fantanaru, @peterjones and all,

                @neculai-i.-fantanaru, why don’t you use the Edit > Line operations > Reverse line order, in order to achieve your goal ?

                So from this selected bunch of text, below :

                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
                
                • Run the Edit > Line operations > Reverse line order option

                => You should get, at once , your expected result !

                masini-unelte-emil-botez-carte-p1_compress_Page_162.jpg
                masini-unelte-emil-botez-carte-p1_compress_Page_161.jpg
                masini-unelte-emil-botez-carte-p1_compress_Page_160.jpg
                masini-unelte-emil-botez-carte-p1_compress_Page_159.jpg
                masini-unelte-emil-botez-carte-p1_compress_Page_158.jpg
                masini-unelte-emil-botez-carte-p1_compress_Page_157.jpg
                masini-unelte-emil-botez-carte-p1_compress_Page_156.jpg
                masini-unelte-emil-botez-carte-p1_compress_Page_155.jpg
                masini-unelte-emil-botez-carte-p1_compress_Page_154.jpg
                masini-unelte-emil-botez-carte-p1_compress_Page_153.jpg
                masini-unelte-emil-botez-carte-p1_compress_Page_152.jpg
                masini-unelte-emil-botez-carte-p1_compress_Page_151.jpg
                

                Best Regards,

                guy038

                PeterJonesP 1 Reply Last reply Reply Quote 0
                • PeterJonesP Online
                  PeterJones @guy038
                  last edited by

                  @guy038 said:

                  why don’t you use

                  Since @neculai-i.-fantanaru said, “I have over 2000 txt files”, that’s not practical.

                  1 Reply Last reply Reply Quote 1
                  • guy038G Offline
                    guy038
                    last edited by guy038

                    Hi, @neculai-i.-fantanaru, @peterjones and all,

                    However, Peter, I suppose that @neculai-i.-fantanaru could create a Python script which :

                    • Builds a list of all the concerned files

                    • Applies the Edit > Line operations > Reverse line order option for each file ( or a selected part of it ) of the built list

                    • Re-saves all the modified files, on turn

                    BR

                    guy038

                    PeterJonesP Neculai I. FantanaruN 2 Replies Last reply Reply Quote 1
                    • PeterJonesP Online
                      PeterJones @guy038
                      last edited by

                      @guy038 said:

                      However, Peter, I suppose that @neculai-i.-fantanaru could create a Python script which

                      If the line reversing is actually what the user wanted. But that user already specifically rejected PythonScript, as they already had a script that did the weird digit reorder that the eventual regex did (rather than reordering the numbers, like the title and original description indicated).

                      So, as far as I can tell: the user wanted a regex (no reason given, but I guess it’s so it could be find-in-files rather than plugin), and apparently wanted a digit duplication/reorder, rather than the reverse sort that was claimed in the title and original description.

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

                        @guy038 I can creat a Python script :)

                        but the beauty and art was finding a solution just with regex.

                        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