Viml matching: Difference between revisions
From wikinotes
No edit summary |
No edit summary |
||
Line 36: | Line 36: | ||
</syntaxhighlight> | </syntaxhighlight> | ||
</blockquote><!-- match string --> | </blockquote><!-- match string --> | ||
=== comparison === | |||
== comparison == | |||
<blockquote> | <blockquote> | ||
<source lang="vim"> | <source lang="vim"> | ||
Line 53: | Line 51: | ||
</source> | </source> | ||
</blockquote> | </blockquote> | ||
</blockquote><!-- Functions --> | |||
== lookahead/lookbehind == | == Syntax == | ||
<blockquote> | |||
=== lookahead/lookbehind === | |||
<blockquote> | <blockquote> | ||
<syntaxhighlight lang="vim"> | <syntaxhighlight lang="vim"> | ||
Line 68: | Line 69: | ||
</blockquote> | </blockquote> | ||
== lookaround == | === lookaround === | ||
<blockquote> | <blockquote> | ||
lookaround allows you to match/ignore strings. They do not become a part of the resulting match. | lookaround allows you to match/ignore strings. They do not become a part of the resulting match. | ||
Line 88: | Line 89: | ||
See https://vi.stackexchange.com/questions/3036/is-lookaround-possible-in-vims-regex-system | See https://vi.stackexchange.com/questions/3036/is-lookaround-possible-in-vims-regex-system | ||
</blockquote> | </blockquote> | ||
</blockquote><!-- Regex --> | </blockquote><!-- Regex --> | ||
</blockquote><!-- Syntax --> |
Revision as of 03:49, 7 January 2023
Regex
NOTE:
When using regex you probably want to use
'literal-strings'
instead of"strings"
.Documentation
vim regex docs http://vimdoc.sourceforge.net/htmldoc/pattern.html#pattern vim regex 101 http://vimregex.com/
Functions
substitution
" equiv to /s/{2}/{3}/g substitute("/path/to/project/src/main/java/com/willpittman/package/MyClass.java", '^.*src/\(main\|test\)/java/', "", "g")match string
echo match('--abc--abc--', 'abc') " => 2 " Getting multiple match positions echo match('--abc--abc--', 'abc', 1) " => 2 (get 1st match) echo match('--abc--abc--', 'abc', 2) " => 7 (get 2nd match) echo match('--abc--abc--', 'abc', 3) " => -1 (get 3rd match -- no matches left)comparison
if match("string", "^str") " regex match '\v...' " all non char ranges have special meaning '\V...' " only '\' has special meaning if "abc" =~ '^ab' " regex match if "ABC" =~? '^ab' " case-insensitive regex if "ABC" ==? '^ab' " case-sensitive regex (regardless of :set (no)ignorecase) if "ABC" ==# '^ab' " case-insensitive regex (regardless of :set (no)ignorecase) " .. there are more ..Syntax
lookahead/lookbehind
" lookahead (exclude vs include) syntax 'apple \(orange\)\@!' " match apple (consume apple), only if orange does not follows it syntax 'apple \(orange\)\@=' " match apple (consume apple), only if orange follows it " lookbehind (exclude vs include) syntax '\(apple\)\@<! orange' " match orange (consume orange), only if not preceeded by apple syntax '\(apple\)\@<= orange' " match orange (consume orange), only if preceeded by applelookaround
lookaround allows you to match/ignore strings. They do not become a part of the resulting match.
{1}\zs{2}
match {2} is preceeded by {1}. {1} is not part of the match{1}\ze{2}
match {1} is followed by {2}. {2} is not part of the match%s/myFunction(\zs.*\ze)/foo/
See https://vi.stackexchange.com/questions/3036/is-lookaround-possible-in-vims-regex-system wed by {2}. {2} is not part of the match
%s/myFunction(\zs.*\ze)/foo/
See https://vi.stackexchange.com/questions/3036/is-lookaround-possible-in-vims-regex-system