I expect this to select all non-alphanumeric characters:
Instead it selects all non-w's.
How can I write this so that it's compatible with vim?
Vi and Vim Stack Exchange is a question and answer site for people using the vi and Vim families of text editors. It only takes a minute to sign up.Sign up to join this community
[..] form is known as a collection (
:h /) and can contain a sequence of characters and/or one or more ranges of characters, separated with
-. It will match any single character that is contained in the sequence/range(s). You can, of course, also match the inverse with
:h white-space you'll find a collection equivalent to
\w word character: [0-9A-Za-z_]
So one approach is to invert/negate that equivalent:
Alternatively, collections can also contain the "character classes" found under
:h E944. These are the same character classes that have been around forever in the BRE/ERE patterns used by legacy tools and POSIX environments.
Alphanumeric chars have the following character class equivalent:
[:alnum:] isalnum ASCII letters and digits
You can invert/negate that like so:
(You could also probably use negative lookarounds with
\w but that's relatively difficult compared to this straightforward analog to
Update: I just realized I didn't address the full pattern you posted. Just look for a character class that's equivalent to
\s and you'll find
" is just
One other thing, this is likely to be the most performant of any possible solutions since others have lookarounds or alternations (
|) and those are expensive.