Community
    • Login

    Proper syntax for \s (spaces) in replace expression

    Scheduled Pinned Locked Moved Help wanted · · · – – – · · ·
    17 Posts 6 Posters 4.2k Views
    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.
    • Hank KH
      Hank K @Coises
      last edited by Hank K

      @Coises & Peter Jones

      Thanks guys !!

      Still clueless about the \x20 ?

      CoisesC 1 Reply Last reply Reply Quote 0
      • CoisesC
        Coises @Hank K
        last edited by

        @Hank-K said in Proper syntax for \s (spaces) in replace expression:

        Still clueless about the \x20

        https://npp-user-manual.org/docs/searching/#match-by-character-code

        1 Reply Last reply Reply Quote 0
        • Hank KH
          Hank K @Coises
          last edited by

          @Coises said in Proper syntax for \s (spaces) in replace expression:

          ( - $1)

          I previously tried the parentheses but no joy there?

          https://regex101.com/r/gDxDVz/1

          CoisesC 1 Reply Last reply Reply Quote 0
          • CoisesC
            Coises @Hank K
            last edited by

            @Hank-K said in Proper syntax for \s (spaces) in replace expression:

            @Coises said in Proper syntax for \s (spaces) in replace expression:

            ( - $1)

            I previously tried the parentheses but no joy there?

            https://regex101.com/r/gDxDVz/1

            All I can say is, try it in Notepad++ rather than in that website. None of the language flavors there appear to be an entirely accurate reproduction of the find and replace syntax used in Notepad++.

            Hank KH 1 Reply Last reply Reply Quote 0
            • Hank KH
              Hank K @Coises
              last edited by

              @Coises said in Proper syntax for \s (spaces) in replace expression:

              All I can say is, try it in Notepad++ rather than in that website. None of the language flavors there appear to be an entirely accurate reproduction of the find and replace syntax used in Notepad++.

              Winner, winner, chicken dinner !!

              Works in Notepad++.

              Lights still dim regarding \x … maybe in future it will become brighter.

              FYI: Will do all future testing directly in Notepad++

              Thanks for hangin with me through my regex growth … lol

              CoisesC 1 Reply Last reply Reply Quote 0
              • CoisesC
                Coises @Hank K
                last edited by

                @Hank-K said in Proper syntax for \s (spaces) in replace expression:

                Lights still dim regarding \x … maybe in future it will become brighter.

                \x just means “the next two characters are going to be two hexadecimal digits that represent the bit pattern of the character I want.”

                Hexadecimal 20 is the bit pattern for an ASCII space. See: https://en.wikipedia.org/wiki/ASCII#Character_set

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

                  @Hank-K said in Proper syntax for \s (spaces) in replace expression:

                  ( - $1)

                  I previously tried the parentheses but no joy there?

                  Joy: To use parentheses in Replace with: field, you must escape them, e.g. \( and \),

                  1 Reply Last reply Reply Quote 0
                  • mkupperM
                    mkupper @Hank K
                    last edited by

                    @Hank-K said in Proper syntax for \s (spaces) in replace expression:

                    Find:	\s{3}-\s([\S])
                    Replc:	  - $1
                    

                    Needing to replace 3 spaces preceding the hyphen with 2 spaces. Searched but no joy in finding how you express \s in the Replace with: instead of using actual white spaces.

                    Unfortunately there are only two ways to put a space in the replace part of a regular expression. Those are an actual space like what you used or the sequence \x20. Shortcuts such as \s and \x20{2} don’t work in the replacement part.

                    Now, there something that could work for you which is
                    Find: \x20(?=\x20\x20-)
                    Replace: nothing

                    In the find part we are looking for a single space followed by a lookahead that has two spaces and the hyphen.
                    The replacement part only affects the single leading space and won’t replace the pattern that is inside the lookahead.

                    Do a search for \x20(?=\x20\x20-). You will see that the first space becomes selected and the cursor is at the end of it. The search does not seem to find the following two spaces and the hyphen but you will discover that it is including them as part of the overall match. You can experiment with both this expression and with the your to get a better understanding of how lookahead works.

                    \x20 is the internal code for a space in both ASCII and Unicode. You will often see \x20 in regular expressions as it’s easy to miss a space, particularly at the very beginning or end of the expression.

                    Do a search for \x20 in Notepad++ and you’ll see that it finds spaces.

                    Don’t use \s unless you also intend to find tabs plus a rather long list of other characters such as form feeds. \s tends to match anything you can’t see…

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

                      Hello, @hank-k, @alan-kilborn, @peterjones, @coises and All,

                      And here is my solution :

                      SEARCH \x20{3}(?=-)

                      REPLACE \x20\x20

                      OR

                      SEARCH \x20{3}(?=-)

                      REPLACE ( )

                      There are two space chars between the parentheses !

                      Best Regards,

                      guy038

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

                        @guy038 said in Proper syntax for \s (spaces) in replace expression:

                        REPLACE ( )

                        There are two space chars between the parentheses !

                        Tricky. :-)
                        The parens are only used because the OP was “uncomfortable” with whitespace he couldn’t really see in the Replace with box.

                        1 Reply Last reply Reply Quote 2
                        • First post
                          Last post
                        The Community of users of the Notepad++ text editor.
                        Powered by NodeBB | Contributors