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.
    • PeterJonesP
      PeterJones @Hank K
      last edited by PeterJones

      @Hank-K ,

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

      I do not think that regex FIND means what you think it means.

      1f8660db-03ea-4fc3-af1e-6395512fd476-image.png

      The document I showed has two matches:

      1. The first 6 characters of line 1
      2. The LF of the CRLF at the end of line 2 plus the CRLF on line 3 (for a total of three spaces), then the hyphen and space and t on line 4.

      I am guessing you only expected it to match one (on line 1).

      \s in the search means “any whitespace character, including spaces, tabs, and newlines”. Thus, it will find three spaces followed by a hyphen then a space then a non-whitespace (like the T in Three spaces). But it will also find three newlines followed by a hyphen followed by a whitespace followed by a non-whitespace (like the t of three newlines).

      If you want to restrict the search to just horizontal whitespace (like space or tab), then use \h instead of \s. And if you want to restrict the search to just ASCII 32 space characters, then use \x20 in the FIND just like @Alan-Kilborn suggested for the replace.

      Also, ([\S]) is completely identical to (\S) – you don’t need to wrap the \S in another character class.

      So if you want to match 3 ASCII 32 space characters, followed by a hyphen and another ASCII 32 space character, followed by something that’s not whitespace (putting that something in a capture group), then you want to FIND = \x32{3}-\x32(\S)

      ----

      Useful References

      • Notepad++ Online User Manual: Searching/Regex
      • FAQ: Where to find other regular expressions (regex) documentation
      Hank KH 1 Reply Last reply Reply Quote 0
      • Hank KH
        Hank K
        last edited by Hank K

        This post is deleted!
        CoisesC 1 Reply Last reply Reply Quote 0
        • Hank KH
          Hank K
          last edited by Hank K

          This post is deleted!
          1 Reply Last reply Reply Quote 0
          • Hank KH
            Hank K @PeterJones
            last edited by

            @PeterJones

            I’m mainly interested in what I can use in the SUBSTITUTION field. In case of nnp, the Replace with:. Instead of horizontal whitespaces ??

            Yes you were correct about ([\S]), was a copy/paste, wasn’t thinking. Didn’t dawn on me till you mention it .

            Thanks for pointing out the \h (lights got brighter here).

            https://regex101.com/r/YDgo5f/2

            f1afea5c-14ac-49c5-85fc-1d8985e85cf7-image.png

            Don’t like my Replace: starting or ending with horizontal whitespaces. Is there a way to encapsulate?

            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:

              Don’t like my Replace: starting or ending with horizontal whitespaces. Is there a way to encapsulate?

              Find what: \h(\h\h-\h\S)
              Replace with: $1

              … if you really did mean to allow for any horizontal white space (in ASCII, that means tabs as well as blanks), removing the first one. If you really mean blanks, and blanks only, use \x20, as @Alan-Kilborn suggested.

              If you only care about beginning and ending, you can use ( - $1) — parentheses are not included as literal characters in replacements unless escaped.

              Hank KH 2 Replies Last reply Reply Quote 0
              • 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