Using /
or ?
enables to find a match for a word in vim. But how can I find an exact match?
For example, my text contains the following words: a a
You can search for aa[^a]
, which will find your two a
s and nothing else.
EDIT: oh boy, y'all are after an exact match :-) So, to match exactly two aa
s and nothing else:
[^a]\zsaa\ze[^a]\|^\zsaa\ze$\|^\zsaa\ze[^a]\|[^a]\zsaa\ze$
And the branches expanded out:
[^a]\zsaa\ze[^a]
\|^\zsaa\ze$
\|^\zsaa\ze[^a]
\|[^a]\zsaa\ze$
These cover all contingencies--the aa
s can be at the beginning, the middle, or the end of any line. And there can't be more than two a
s together.
\zs
means the actual match starts here\ze
means the actual match ends hereaa
in a line surrounded by other charactersaa
when it makes up the whole lineaa
at the beginning of a lineaa
at the end of a line.My mind boggles at fancy things like look-behind assertions, so I tried to stick to reasonably simple regex concepts.
EDIT 2: see @benjifisher's simplified, more elegant version below for your intellectual pleasure.