Wikia

Vim Tips Wiki

Changes: Match every word except foo

Edit

Back to page

(Adjust previous/next navigation)
(Change <tt> to <code>, perhaps also minor tweak.)
Line 4: Line 4:
 
|previous=219
 
|previous=219
 
|next=222
 
|next=222
|created=February 25, 2002
+
|created=2002
 
|complexity=intermediate
 
|complexity=intermediate
 
|author=Michael Geddes
 
|author=Michael Geddes
Line 17: Line 17:
 
</pre>
 
</pre>
   
*<tt>\v</tt> Very magic
+
*<code>\v</code> Very magic
*<tt><</tt> Start-of-word
+
*<code><</code> Start-of-word
*<tt>(Foo>)</tt> The atom 'Foo' followed by end-of-word
+
*<code>(Foo>)</code> The atom 'Foo' followed by end-of-word
*<tt>@!</tt> Match (with zero length) when the previous atom doesn't match.
+
*<code>@!</code> Match (with zero length) when the previous atom doesn't match.
*<tt>\k+</tt> Match one or more Keywords
+
*<code>\k+</code> Match one or more Keywords
*<tt>></tt> Match end-of-word.
+
*<code>></code> Match end-of-word.
   
 
The non-magic version is:
 
The non-magic version is:
Line 30: Line 30:
 
</pre>
 
</pre>
   
The use of <tt>\@!</tt> can be very tricky. According to the Vim help files, it is often easier to use <tt>\@<!</tt> instead. For example, to find all 'bar' strings unless they are part of 'foobar', use the following (non-magic):
+
The use of <code>\@!</code> can be very tricky. According to the Vim help files, it is often easier to use <code>\@<!</code> instead. For example, to find all 'bar' strings unless they are part of 'foobar', use the following (non-magic):
 
<pre>
 
<pre>
 
\(foo\)\@<!bar
 
\(foo\)\@<!bar

Revision as of 05:19, July 13, 2012

Duplicate tip

This tip is very similar to the following:

These tips need to be merged – see the merge guidelines.

Tip 220 Printable Monobook Previous Next

created 2002 · complexity intermediate · author Michael Geddes · version 6.0


This is a regular expression that matches all words except 'foo'

\v<(foo>)@!\k+>
  • \v Very magic
  • < Start-of-word
  • (Foo>) The atom 'Foo' followed by end-of-word
  • @! Match (with zero length) when the previous atom doesn't match.
  • \k+ Match one or more Keywords
  • > Match end-of-word.

The non-magic version is:

\<\(foo\>\)\@!\k\+\>

The use of \@! can be very tricky. According to the Vim help files, it is often easier to use \@<! instead. For example, to find all 'bar' strings unless they are part of 'foobar', use the following (non-magic):

\(foo\)\@<!bar

References

Comments

Around Wikia's network

Random Wiki