Viml matching: Difference between revisions
From wikinotes
(→Syntax) |
|||
Line 60: | Line 60: | ||
== Syntax == | == Syntax == | ||
<blockquote> | <blockquote> | ||
=== magic modes === | |||
<blockquote> | |||
There are 4x variations of magic-ness that you can choose between to simplify your regexes.<br> | |||
The magicness is indicated by prefixing your search with an escape. | |||
<syntaxhighlight lang="vim"> | |||
/\v${your-regex} | |||
/\m${your-regex} | |||
/\M${your-regex} | |||
/\V${your-regex} | |||
</syntaxhighlight> | |||
You can use this to make your regexes a bit more readable. | |||
</blockquote><!-- magic modes --> | |||
=== lookahead/lookbehind === | === lookahead/lookbehind === | ||
<blockquote> | <blockquote> |
Revision as of 20:03, 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/ :h magic
https://vimhelp.org/pattern.txt.html#%2Fmagic 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 pos
echo match('--abc--abc--', 'abc') " => 2 match start-pos echo matchend('--abc--abc--', 'abc') " => 5 match end-pos" 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
magic modes
There are 4x variations of magic-ness that you can choose between to simplify your regexes.
The magicness is indicated by prefixing your search with an escape./\v${your-regex} /\m${your-regex} /\M${your-regex} /\V${your-regex}You can use this to make your regexes a bit more readable.
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