IF in a Regex expresion? (replace something in file names ONLY if file type is xxx)











up vote
-1
down vote

favorite












Ok, I know some regex but this fooling me...



I usually manage every month hundreds of files submited and have to make some checks and replaces before making them available again in our intranet...



I'm doing it locally on hd on Windows, through a file renamer program which can do pcre but only do one line at a time, so all should be in the same regex.



The problem is that I would like to do replacements only if file type is xxx.



For example, replace all spaces for underscores ONLY if extension is jpg|jpeg|jpe



so




this is a test.jpg => this_is_a_test.jpg



this is a another test.jpe => this_is_a_another_test.jpe



this is a test.docx => this is a test.docx




Jpg is an EXAMPLE, I do diferent replaces for each extension and not for all extensions, so something which replaces spaces in the above example in the .docx will be wrong...



is it posible???










share|improve this question


























    up vote
    -1
    down vote

    favorite












    Ok, I know some regex but this fooling me...



    I usually manage every month hundreds of files submited and have to make some checks and replaces before making them available again in our intranet...



    I'm doing it locally on hd on Windows, through a file renamer program which can do pcre but only do one line at a time, so all should be in the same regex.



    The problem is that I would like to do replacements only if file type is xxx.



    For example, replace all spaces for underscores ONLY if extension is jpg|jpeg|jpe



    so




    this is a test.jpg => this_is_a_test.jpg



    this is a another test.jpe => this_is_a_another_test.jpe



    this is a test.docx => this is a test.docx




    Jpg is an EXAMPLE, I do diferent replaces for each extension and not for all extensions, so something which replaces spaces in the above example in the .docx will be wrong...



    is it posible???










    share|improve this question
























      up vote
      -1
      down vote

      favorite









      up vote
      -1
      down vote

      favorite











      Ok, I know some regex but this fooling me...



      I usually manage every month hundreds of files submited and have to make some checks and replaces before making them available again in our intranet...



      I'm doing it locally on hd on Windows, through a file renamer program which can do pcre but only do one line at a time, so all should be in the same regex.



      The problem is that I would like to do replacements only if file type is xxx.



      For example, replace all spaces for underscores ONLY if extension is jpg|jpeg|jpe



      so




      this is a test.jpg => this_is_a_test.jpg



      this is a another test.jpe => this_is_a_another_test.jpe



      this is a test.docx => this is a test.docx




      Jpg is an EXAMPLE, I do diferent replaces for each extension and not for all extensions, so something which replaces spaces in the above example in the .docx will be wrong...



      is it posible???










      share|improve this question













      Ok, I know some regex but this fooling me...



      I usually manage every month hundreds of files submited and have to make some checks and replaces before making them available again in our intranet...



      I'm doing it locally on hd on Windows, through a file renamer program which can do pcre but only do one line at a time, so all should be in the same regex.



      The problem is that I would like to do replacements only if file type is xxx.



      For example, replace all spaces for underscores ONLY if extension is jpg|jpeg|jpe



      so




      this is a test.jpg => this_is_a_test.jpg



      this is a another test.jpe => this_is_a_another_test.jpe



      this is a test.docx => this is a test.docx




      Jpg is an EXAMPLE, I do diferent replaces for each extension and not for all extensions, so something which replaces spaces in the above example in the .docx will be wrong...



      is it posible???







      regex pcre






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked yesterday









      user3393366

      384




      384
























          2 Answers
          2






          active

          oldest

          votes

















          up vote
          2
          down vote



          accepted










          You need to find spaces, and then look ahead to see the extension:



           (?=.*.jp(?:g|e)$)


          Note the leading space.



          Try it here: https://regex101.com/r/ZgDv7S/1






          share|improve this answer





















          • YES, IS THAT!!!
            – user3393366
            yesterday


















          up vote
          0
          down vote













          You said you were on Windows, but do you have WSL (Bash for Linux) or something similar available? If so, here is a quick way to do this from the Linux command line:



          rename 's/ /_/g;' *.jpg *.jpe


          Explanation: rename runs the given Perl script on the name of the specified files.






          share|improve this answer





















            Your Answer






            StackExchange.ifUsing("editor", function () {
            StackExchange.using("externalEditor", function () {
            StackExchange.using("snippets", function () {
            StackExchange.snippets.init();
            });
            });
            }, "code-snippets");

            StackExchange.ready(function() {
            var channelOptions = {
            tags: "".split(" "),
            id: "1"
            };
            initTagRenderer("".split(" "), "".split(" "), channelOptions);

            StackExchange.using("externalEditor", function() {
            // Have to fire editor after snippets, if snippets enabled
            if (StackExchange.settings.snippets.snippetsEnabled) {
            StackExchange.using("snippets", function() {
            createEditor();
            });
            }
            else {
            createEditor();
            }
            });

            function createEditor() {
            StackExchange.prepareEditor({
            heartbeatType: 'answer',
            convertImagesToLinks: true,
            noModals: true,
            showLowRepImageUploadWarning: true,
            reputationToPostImages: 10,
            bindNavPrevention: true,
            postfix: "",
            imageUploader: {
            brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
            contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
            allowUrls: true
            },
            onDemand: true,
            discardSelector: ".discard-answer"
            ,immediatelyShowMarkdownHelp:true
            });


            }
            });














             

            draft saved


            draft discarded


















            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53238561%2fif-in-a-regex-expresion-replace-something-in-file-names-only-if-file-type-is-x%23new-answer', 'question_page');
            }
            );

            Post as a guest
































            2 Answers
            2






            active

            oldest

            votes








            2 Answers
            2






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes








            up vote
            2
            down vote



            accepted










            You need to find spaces, and then look ahead to see the extension:



             (?=.*.jp(?:g|e)$)


            Note the leading space.



            Try it here: https://regex101.com/r/ZgDv7S/1






            share|improve this answer





















            • YES, IS THAT!!!
              – user3393366
              yesterday















            up vote
            2
            down vote



            accepted










            You need to find spaces, and then look ahead to see the extension:



             (?=.*.jp(?:g|e)$)


            Note the leading space.



            Try it here: https://regex101.com/r/ZgDv7S/1






            share|improve this answer





















            • YES, IS THAT!!!
              – user3393366
              yesterday













            up vote
            2
            down vote



            accepted







            up vote
            2
            down vote



            accepted






            You need to find spaces, and then look ahead to see the extension:



             (?=.*.jp(?:g|e)$)


            Note the leading space.



            Try it here: https://regex101.com/r/ZgDv7S/1






            share|improve this answer












            You need to find spaces, and then look ahead to see the extension:



             (?=.*.jp(?:g|e)$)


            Note the leading space.



            Try it here: https://regex101.com/r/ZgDv7S/1







            share|improve this answer












            share|improve this answer



            share|improve this answer










            answered yesterday









            Sweeper

            59.9k967134




            59.9k967134












            • YES, IS THAT!!!
              – user3393366
              yesterday


















            • YES, IS THAT!!!
              – user3393366
              yesterday
















            YES, IS THAT!!!
            – user3393366
            yesterday




            YES, IS THAT!!!
            – user3393366
            yesterday












            up vote
            0
            down vote













            You said you were on Windows, but do you have WSL (Bash for Linux) or something similar available? If so, here is a quick way to do this from the Linux command line:



            rename 's/ /_/g;' *.jpg *.jpe


            Explanation: rename runs the given Perl script on the name of the specified files.






            share|improve this answer

























              up vote
              0
              down vote













              You said you were on Windows, but do you have WSL (Bash for Linux) or something similar available? If so, here is a quick way to do this from the Linux command line:



              rename 's/ /_/g;' *.jpg *.jpe


              Explanation: rename runs the given Perl script on the name of the specified files.






              share|improve this answer























                up vote
                0
                down vote










                up vote
                0
                down vote









                You said you were on Windows, but do you have WSL (Bash for Linux) or something similar available? If so, here is a quick way to do this from the Linux command line:



                rename 's/ /_/g;' *.jpg *.jpe


                Explanation: rename runs the given Perl script on the name of the specified files.






                share|improve this answer












                You said you were on Windows, but do you have WSL (Bash for Linux) or something similar available? If so, here is a quick way to do this from the Linux command line:



                rename 's/ /_/g;' *.jpg *.jpe


                Explanation: rename runs the given Perl script on the name of the specified files.







                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered yesterday









                bitinerant

                343




                343






























                     

                    draft saved


                    draft discarded



















































                     


                    draft saved


                    draft discarded














                    StackExchange.ready(
                    function () {
                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53238561%2fif-in-a-regex-expresion-replace-something-in-file-names-only-if-file-type-is-x%23new-answer', 'question_page');
                    }
                    );

                    Post as a guest




















































































                    Popular posts from this blog

                    Bressuire

                    Vorschmack

                    Quarantine