vim 正则表达式非贪婪匹配
- 运维笔记
- 2020-11-20
- 369热度
- 0评论
问题
vim 中不能使用 .*?。
解法
可使用 .\{-},代替 .*,实现非贪婪匹配。
详细说明
输入 :help non-greedy,可以看到如下帮助文档:
non-greedy If a "-" appears immediately after the "{", then a shortest match first algorithm is used (see example below). In particular, "\{-}" is the same as "*" but uses the shortest match first algorithm. BUT: A match that starts earlier is preferred over a shorter match: "a\{-}b" matches "aaab" in "xaaab". Example matches ab\{2,3}c "abbc" or "abbbc" a\{5} "aaaaa" ab\{2,}c "abbc", "abbbc", "abbbbc", etc. ab\{,3}c "ac", "abc", "abbc" or "abbbc" a[bc]\{3}d "abbbd", "abbcd", "acbcd", "acccd", etc. a\(bc\)\{1,2}d "abcd" or "abcbcd" a[bc]\{-}[cd] "abc" in "abcd" a[bc]*[cd] "abcd" in "abcd" The } may optionally be preceded with a backslash: \{n,m\}.
注意:实测,/\v 开头进行查找时,无法使用非贪婪模式。应该只使用 /。
参考链接:
https://stackoverflow.com/questions/1305853/how-can-i-make-my-match-non-greedy-in-vim