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).
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. Eg 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'.
\\&
can be used to match a line containing two (or more) words in any order. For example,
/.*one\\&.*two\\&.*three
will find lines containing one
, two
and three
in any order. The .*
is necessary because each branch must start matching in the same place.
Note, the last branch is the one that participates in any substitution. For example, applying the following substitution:
s/.*one\\&.*two\\&.*three/<&>/
on the line
The numbers three, two, and one
results in
<The numbers three>, two, and one
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.