What is the \& pattern in Vim's Regex

Explanation:

\& is to \|, what the and operator is to the or operator. Thus, both concats have to match, but only the last will be highlighted.

Example 1:

(The following tests assume :setlocal hlsearch.)

Imagine this string:

foo foobar

Now, /foo will highlight foo in both words. But sometimes you just want to match the foo in foobar. Then you have to use /foobar\&foo.

That's how it works anyway. Is it often used? I haven't seen it more than a few times so far. Most people will probably use zero-width atoms in such simple cases. E.g. the same as in this example could be done via /foo\zebar.

Example 2:

/\c\v([^aeiou]&\a){4}.

\c - ignore case

\v - "very magic" (-> you don't have to escape the & in this case)

(){4} - repeat the same pattern 4 times

[^aeiou] - exclude these characters

\a - alphabetic character

Thus, this, rather confusing, regexp would match xxxx, XXXX, wXyZ or WxYz but not AAAA or xxx1. Putting it in simple terms: Match any string of 4 alphabetic characters that doesn't contain either 'a', 'e', 'i', 'o' or 'u'.

Tags:

Vim

Regex