Community
    • Login

    Replacing all spaces between a string and the newline character

    Scheduled Pinned Locked Moved Help wanted · · · – – – · · ·
    6 Posts 3 Posters 1.8k Views 1 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.
    • kiynK Offline
      kiyn
      last edited by

      I need to replace all spaces, only in the lines that contain the string, only from the string to the newline character.
      I couldn’t find a satisfactory expression.
      The string is: FILE:///
      Every space should be replaced with: %20

      Is it possible to do this?
      Can anyone help me?
      many thanks
      thanks a lot

      Alan KilbornA 1 Reply Last reply Reply Quote 0
      • Alan KilbornA Offline
        Alan Kilborn @kiyn
        last edited by Alan Kilborn

        @kiyn

        Find: (?-s)(?-i:FILE:///|(?!\A)\G).*?\K\x20
        Replace: %20
        Search mode: Regular expression

        Reference HERE.

        kiynK 1 Reply Last reply Reply Quote 4
        • kiynK Offline
          kiyn @Alan Kilborn
          last edited by

          @Alan-Kilborn

          Thank you very much, it works perfectly!

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

            Hello @kiyn, @alan-kilborn and All,

            The @alan-kilborn’s search regex is based on the generic regex below :

            https://community.notepad-plus-plus.org/topic/22690/generic-regex-replacing-in-a-specific-zone-of-text/

            Which allows to replace any string with an other one, ONLY IF that string is found between two boundaries which may be located in a same line or between two different lines

            To be rigourous, I slightly modified the @alan-kilborn’s formulation in order to match all the possible cases !


            So, let’s suppose this INPUT text :

            file:///D:\Documentation\Musique\Marilyne\Amy Winehouse - Back To Black.mp3
            
            first Test
            
            Second test file:///D:\Documentation\Musique\Marilyne\Jason Marz|I'm Yours.mp3
            
            FILE:///D:\Documentation\Musique\Marilyne\Norah Jones - Toes.MP3. Third test
            
            Fourth test file:///D:\Documentation\Musique\Marilyne\Scorpions - Still Loving You.mp3 fifth and final test
            
            file:///D:\Documentation\Musique\Marilyne\Norah Jones - Wake Me Up.MP3
            

            My version is :

            • SEARCH (?i:FILE:///|(?!\A)\G)(?i:(?!\.mp3).)*?\K\x20

            • REPLACE %20

            And we get, after clicking on the Replace All button ONCE ONLY, the expected OUTPUT text, below :

            file:///D:\Documentation\Musique\Marilyne\Amy%20Winehouse%20-%20Back%20To%20Black.mp3
            
            first Test
            
            Second test file:///D:\Documentation\Musique\Marilyne\Jason%20Marz|I'm%20Yours.mp3
            
            FILE:///D:\Documentation\Musique\Marilyne\Norah%20Jones%20-%20Toes.MP3. Third test
            
            Fourth test file:///D:\Documentation\Musique\Marilyne\Scorpions%20-%20Still%20Loving%20You.mp3 fifth and final test
            
            file:///D:\Documentation\Musique\Marilyne\Norah%20Jones%20-%20Wake%20Me%20Up.MP3
            

            You’ll note that ONLY the space chars, between file:/// or FILE:/// and .mp3 or .MP3, are replaced by the %20 string


            If you don’t use the \G syntax, you may also run this simplified version :

            • SEARCH (?i)FILE:///(?:(?!\.mp3).)+?\K\x20

            • REPLACE %20

            But, unlike with the behaviour of the previous regex, you’ll need, this time, to click several times on the Replace All button till you get the message : Replace All: 0 occurrences were replaced ... :-)

            Best Regards,

            guy038

            Alan KilbornA 1 Reply Last reply Reply Quote 1
            • Alan KilbornA Offline
              Alan Kilborn @guy038
              last edited by

              Note that @guy038 introduced the notion that this is somehow related to mp3 files; I see no such restriction introduced by OP.

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

                Hi @kiyn, @alan-kilborn and All,

                Ah… You right about it, Alan. Sorry, I was concentrating on my example !


                So, regarding the general case and to be rigourous, we should use the generic regular expression itself :

                • SEARCH (?-si:BSR|(?!\A)\G)(?s-i:(?!ESR).)*?\K(?-si:FR)

                • REPLACE RR

                where :

                FR = \x20 ( a space char )

                RR = %20

                BSR = FILE:///

                ESR = The end of any link, beginning with file:/// or FILE:/// : for example, \.mp3 or \.com|\.us or simply \.\w{2,}


                Notes :

                • We can replace the (?-si: modifiers, at the beginning of the regex, by the insensitive modifier (?i: only

                • We can omit the (?s-i: modifiers, in the middle of the regex, and keep only the non-capturing group (?:(?!ESR).)

                • We can omit the (?-si: modifiers and the non-capturing group, all together, at the end of the regex

                So the generic regex can simply be expressed as :

                • SEARCH (?i:BSR|(?!\A)\G)(?:(?!ESR).)*?\KFR

                • REPLACE RR


                Thus, the functional regex S/R becomes :

                • SEARCH (?i:FILE:///|(?!\A)\G)(?:(?!\.\w{2,}).)*?\K\x20

                • REPLACE %20

                And from this INPUT text :

                file:///D:\Documentation\Musique\Marilyne\Amy Winehouse - Back To Black.com
                
                first Test
                
                Second test file:///D:\Documentation\Musique\Marilyne\Jason Marz|I'm Yours.fr
                
                FILE:///D:\Documentation\Musique\Marilyne\Norah Jones - Toes.us. Third test
                
                Fourth test file:///D:\Documentation\Musique\Marilyne\Scorpions - Still Loving You.gouv fifth and final test
                
                file:///D:\Documentation\Musique\Marilyne\Norah Jones - Wake Me Up.MP3
                

                We get this OUTPUT text :

                file:///D:\Documentation\Musique\Marilyne\Amy%20Winehouse%20-%20Back%20To%20Black.com
                
                first Test
                
                Second test file:///D:\Documentation\Musique\Marilyne\Jason%20Marz|I'm%20Yours.fr
                
                FILE:///D:\Documentation\Musique\Marilyne\Norah%20Jones%20-%20Toes.us. Third test
                
                Fourth test file:///D:\Documentation\Musique\Marilyne\Scorpions%20-%20Still%20Loving%20You.gouv fifth and final test
                
                file:///D:\Documentation\Musique\Marilyne\Norah%20Jones%20-%20Wake%20Me%20Up.MP3
                

                BR

                guy038

                1 Reply Last reply Reply Quote 1

                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