What is the \& pattern in Vim's Regex
regex, vim
Solution
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'.
Problem
I recently came across the branch specifier in Vim regex builtins. Vim's help section on `\&` contains this: ``` A branch is one or more concats, separated by "\&". It matches the last concat, but only if all the preceding concats also match at the same position. Examples: "foobeep\&..." matches "foo" in "foobeep". ".*Peter\&.*Bob" matches in a line containing both "Peter" and "Bob" ``` It's not clear how it is used and what it is used for. A good explanation of what it does and how it is used would be great. To be clear this is not the `&` (replace with whole match) used in a substitution, this is the `\&` used in a pattern. Example usage: ``` /\c\v([^aeiou]&\a){4} ``` Used to search for 4 consecutive consonants (Taken from vim tips).