How do I match a pattern only when it is preceded or followed by another pattern using Vim regex?
\@<= and \@=
Vim's regex engine supports zero-width lookahead and lookbehind assertions using the \@ atom.
\@<= and \@=
Vim's regex engine supports zero-width lookahead and lookbehind assertions using the \@ atom.
\%l and \%c
Vim's \%l and \%c pattern atoms anchor a search to a particular line number or column, enabling surgical searches and substitutions that standard regex cannot e
\@= and \@! and \@<= and \@<!
Vim's regex engine supports zero-width lookahead and lookbehind assertions using the \@=, \@!, \@<=, and \@<! atoms.
\v
Vim's default regex mode ("magic") requires backslashes before many special characters: \(, \ , \+, \{.
:s/\v(pattern1)(pattern2)/\2\1/
Vim's substitute command supports capture groups (also called backreferences), which let you rearrange matched portions of text.
/\%>10c\%<20cpattern
When working with columnar data like CSV files, log files, or fixed-width records, you often need to match a pattern only when it appears in a specific column r
/\v(pattern)@<=match
Vim supports zero-width assertions (lookahead and lookbehind) in its regex engine.
search #search #regex #advanced-search #lookahead #lookbehind
/\vfoo\zsbar\ze/
Vim's \zs (start of match) and \ze (end of match) atoms let you control which portion of a pattern is treated as the actual match.
/pattern\zs.*\ze/
Vim's \zs (start of match) and \ze (end of match) atoms let you define a search pattern but only highlight or operate on a portion of it.
/\%5l\%10cpattern
Vim provides position-matching atoms that constrain where a pattern can match based on line numbers, column positions, or virtual columns.
/pattern\{3}
Vim supports counted quantifiers that let you specify exactly how many times a pattern should repeat.
/pattern\@<=match
Vim supports zero-width assertions (lookahead and lookbehind) in its regex engine, allowing you to match text based on what precedes or follows it without inclu
:%s/\(\w\+\) \(\w\+\)/\2 \1/g
Vim's substitute command supports captured groups (back-references) using \( and \), allowing you to capture parts of a match and rearrange them in the replacem
/pattern\_s\+next
Vim's regular expressions support multi-line matching through underscore-prefixed atoms.
/foo\|bar\|baz
Vim's search supports alternation with the \ operator, allowing you to find any one of several patterns in a single search.
/[[:digit:][:upper:]]
Vim supports POSIX character classes inside bracket expressions, providing a portable and readable way to match categories of characters.
/\(function\s\+\)\zs\w\+
Vim's \zs and \ze atoms let you control which part of a matched pattern gets highlighted and operated on.
/\(foo\)\@<=bar
Use \@<= for positive lookbehind.
/\%>10l\%<20lpattern
Vim's \%>Nl and \%10l matches only after line 10 \%10l\%10l\%<20lold/new/g Combine with column restrictions for precise region targeting Tips Line numbers in \%
/\(pattern\)\@<=target or /target\(pattern\)\@=
Vim supports zero-width assertions (lookahead and lookbehind) in its regex engine.