0

This is my find/replace method

sometext([^s])

It thinks the 's' is either an 's' or a '\s' - ie whitespace. How to make it so it definately does not think its a whitespace?

To reproduce this problem: Create a file:

sometexts

sometext

sometext   

Do a search with the above regex, and notice it only finds the last item. The second item is not found.

Steve Tomlin
  • 3,391
  • 3
  • 31
  • 63
  • This s is a real s. A \s would be whitespace. This seems weird – DownloadPizza Sep 14 '21 at 11:16
  • I think you want: `sometext\([^s]+\)` – Tim Biegeleisen Sep 14 '21 at 11:17
  • The parenthesis, though, have a special meaning in regular expressions. What text are you trying to find? Please edit the question and add text samples that match and do not match, as well as the intended replacements. – Álvaro González Sep 14 '21 at 11:17
  • @TimBiegeleisen For clarify - I'm not looking for parenthesis in the text. That's for me to replace the 's' with another character. The problem is vscode is for some reason thinking its either 's' or \s. I need to come up with another solution to get around this bug – Steve Tomlin Sep 14 '21 at 11:21
  • @Steve Try using 2 backslashes `\\s` ... this is necessary in some flavors of regex. – Tim Biegeleisen Sep 14 '21 at 11:23
  • @TimBiegeleisen Thanks. Tried that but no joy – Steve Tomlin Sep 14 '21 at 11:26
  • You are wrong about the regex meaning. All it matches is a `sometext` char sequence and then ANY char other than `s` (note: in VSCode, excluding line breaks). As in the first two cases there is either `s` or line break after `sometext` there is no match - as expected. If all you want is to make sure the `sometext` match is at a word boundary, use `\b`, `sometext\b`. If you need to make sure there is whitespace or end of line/string, use `sometext(?!\S)` – Wiktor Stribiżew Sep 14 '21 at 11:37
  • @WiktorStribiżew It is not wrong. A line break is a character. Therefore it should work as it does in every other regex compiler from javascript to c++ – Steve Tomlin Sep 14 '21 at 14:54
  • VSCode is not like any other compiler. It is a *text editor*. And in text editors, line break matching is tool-specific. Did you use Vim? EmEditor? Atom? Visual Studio? `[^s]` does not match a line break in VSCode regex unless you use `\n` or `\r` somewhere in a regex. – Wiktor Stribiżew Sep 14 '21 at 15:03
  • @WiktorStribiżew Atom works. Vscode doesn't. Its not honouring the regex. – Steve Tomlin Sep 14 '21 at 16:01
  • As I mentioned, there is no universal rule in text editors as far as line break handling. Vim is the weirdest anyway. – Wiktor Stribiżew Sep 14 '21 at 16:02
  • I updated the close reason to a more precise one. In general, we never use a negated character class for that, we use a negative lookahead. – Wiktor Stribiżew Sep 14 '21 at 16:10
  • I don't know the exact library used by Code but many engines treat line endings as special cases and even have flags to work in multiline mode. I think you want `sometext([^s]|$)` or `sometext([^s]|\n)`. – Álvaro González Sep 14 '21 at 16:13

0 Answers0