Proper syntax for \s (spaces) in replace expression
-
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
Don’t like my Replace: starting or ending with horizontal whitespaces. Is there a way to encapsulate?
-
@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-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
-
@Coises said in Proper syntax for \s (spaces) in replace expression:
( - $1)
I previously tried the parentheses but no joy there?
-
@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?
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++.
-
@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
-
@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
-
@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\)
, -
@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: nothingIn 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… -
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
-
@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.