3

I can't find out how to search words with an exclamation mark at the end using boundaries.

p.e. text:

Do this!

search:

/\<this!\>

This doesn't match this! in the text. This either:

/\<this\!\>

I suppose that VIM thinks it must be Lookahead/Lookbehind Zero-Length Assertion.
How can I resolve this?

Reman
  • 7,931
  • 11
  • 55
  • 97

2 Answers2

4

The ! is not a keyword character (in your case, at least), so a regular expression assertion for a keyword to non-keyword boundary (which \> is) won't match after it. What does work is this

/\<this\>!/

But here, the \> is superfluous, as s clearly is a keyword character, and ! isn't. So, only use the assertions where actually needed (like \< at the beginning in your example, to avoid matching foothis!), and all is well.

Alternatively, if for your purposes ! belongs to a word, and you want to match it as such, include it via

:setlocal iskeyword+=!

and use \k for matching inside the word, e.g. \<\k\{5}\> to find all 5-letter (key)words.

Ingo Karkat
  • 167,457
  • 16
  • 250
  • 324
1

Try this (or replace \w with the character class your words consist of):

/\<\w\+\!

The ! is not part of a word so any word boundary match like \> will match before, not after.

speakr
  • 4,141
  • 1
  • 22
  • 28
  • Don't understand why it is not part of a word. Is there no way to include it within the boundaries? – Reman Oct 16 '14 at 09:49
  • @Remonn: you need to understand that a word boundary is the position between a character from the character class `\w` and an other character that is not a member of this class (or the limit of the string). – Casimir et Hippolyte Oct 16 '14 at 09:53