-<!doctype html><html lang=en><head><meta content="IE=edge" http-equiv=X-UA-Compatible><meta content="text/html; charset=utf-8" http-equiv=content-type><meta content="width=device-width,initial-scale=1.0,maximum-scale=1" name=viewport><title>Vim tip 29: greedy quantifiers</title><link href=https://learnbyexample.github.io/atom.xml rel=alternate title=RSS type=application/atom+xml><script src=https://cdnjs.cloudflare.com/ajax/libs/slideout/1.0.1/slideout.min.js></script><link href=https://learnbyexample.github.io/site.css rel=stylesheet><meta content="Vim tip 29: greedy quantifiers" property=og:title><meta content=website property=og:type><meta content=https://learnbyexample.github.io/vim-tip-29/ property=og:url><meta content=@learn_byexample property=twitter:site><link href=https://learnbyexample.github.io/favicon.svg rel=icon><link rel="shortcut icon" href=https://learnbyexample.github.io/favicon.png><body><div class=container><div class=mobile-navbar id=mobile-navbar><div class=mobile-header-logo><a class=logo href=/>learnbyexample</a></div><div class="mobile-navbar-icon icon-out"><span></span><span></span><span></span></div></div><nav class="mobile-menu slideout-menu slideout-menu-left" id=mobile-menu><ul class=mobile-menu-list><li class=mobile-menu-item><a href=https://learnbyexample.github.io/books> Books </a><li class=mobile-menu-item><a href=https://learnbyexample.github.io/mini> Mini </a><li class=mobile-menu-item><a href=https://learnbyexample.github.io/tips> Tips </a><li class=mobile-menu-item><a href=https://learnbyexample.github.io/tags> Tags </a><li class=mobile-menu-item><a href=https://learnbyexample.github.io/about> About </a></ul></nav><header id=header><div class=logo><a href=https://learnbyexample.github.io>learnbyexample</a></div><nav class=menu><ul><li><a href=https://learnbyexample.github.io/books> Books </a><li><a href=https://learnbyexample.github.io/mini> Mini </a><li><a href=https://learnbyexample.github.io/tips> Tips </a><li><a href=https://learnbyexample.github.io/tags> Tags </a><li><a href=https://learnbyexample.github.io/about> About </a></ul></nav></header><main><div class=content id=mobile-panel><article class=post><header class=post__header><h1 class=post__title><a href=https://learnbyexample.github.io/tips/vim-tip-29/>Vim tip 29: greedy quantifiers</a></h1><div class=post__meta><span class=post__time>2023-07-19</span></div></header><div class=post-content><p>Quantifiers can be applied to literal characters, dot metacharacter, groups, backreferences and character classes.<ul><li><code>*</code> match zero or more times <ul><li><code>abc*</code> matches <code>ab</code> or <code>abc</code> or <code>abccc</code> or <code>abcccccc</code> but not <code>bc</code><li><code>Error.*valid</code> matches <code>Error: invalid input</code> but not <code>valid Error</code><li><code>s/a.*b/X/</code> replaces <code>table bottle bus</code> with <code>tXus</code> since <code>a.*b</code> matches from the first <code>a</code> to the last <code>b</code></ul><li><code>\+</code> match one or more times <ul><li><code>abc\+</code> matches <code>abc</code> or <code>abccc</code> but not <code>ab</code> or <code>bc</code></ul><li><code>\?</code> match zero or one times <ul><li><code>\=</code> can also be used, helpful if you are searching backwards with the <code>?</code> command<li><code>abc\?</code> matches <code>ab</code> or <code>abc</code>. This will match <code>abccc</code> or <code>abcccccc</code> as well, but only the <code>abc</code> portion<li><code>s/abc\?/X/</code> replaces <code>abcc</code> with <code>Xc</code></ul><li><code>\{m,n}</code> match <code>m</code> to <code>n</code> times (inclusive) <ul><li><code>ab\{1,4}c</code> matches <code>abc</code> or <code>abbc</code> or <code>xabbbcz</code> but not <code>ac</code> or <code>abbbbbc</code></ul><li><code>\{m,}</code> match at least <code>m</code> times <ul><li><code>ab\{3,}c</code> matches <code>xabbbcz</code> or <code>abbbbbc</code> but not <code>ac</code> or <code>abc</code> or <code>abbc</code></ul><li><code>\{,n}</code> match up to <code>n</code> times (including <code>0</code> times) <ul><li><code>ab\{,2}c</code> matches <code>abc</code> or <code>ac</code> or <code>abbc</code> but not <code>xabbbcz</code> or <code>abbbbbc</code></ul><li><code>\{n}</code> match exactly <code>n</code> times <ul><li><code>ab\{3}c</code> matches <code>xabbbcz</code> but not <code>abbc</code> or <code>abbbbbc</code></ul></ul><p>Greedy quantifiers will consume as <em>much</em> as possible, provided the overall pattern is also matched. That's how the <code>Error.*valid</code> example worked. If <code>.*</code> had consumed everything after <code>Error</code>, there wouldn't be any more characters to try to match <code>valid</code>. How the regexp engine handles matching varying amount of characters depends on the implementation details (backtracking, NFA, etc).<p><img alt=info src=/images/info.svg> See <a href=https://vimhelp.org/pattern.txt.html#pattern-overview>:h pattern-overview</a> for more details.<p><img alt=info src=/images/info.svg> If you are familiar with other regular expression flavors like Perl, Python, etc, you'd be surprised by the use of <code>\</code> in the above examples. If you use <code>\v</code> very magic modifier, the <code>\</code> won't be needed.<p><strong>Video demo</strong>:<p align=center><iframe allow="accelerometer; clipboard-write; encrypted-media; gyroscope; picture-in-picture" title="YouTube video player" allowfullscreen frameborder=0 height=315 loading=lazy src=https://www.youtube.com/embed/pg1OX4pHN8M width=560></iframe></p><br><p><img alt=info src=/images/info.svg> See also my <a href=https://github.com/learnbyexample/vim_reference>Vim Reference Guide</a> and <a href=https://learnbyexample.github.io/curated_resources/vim.html>curated list of resources for Vim</a>.</div><div class=post-footer><div class=post-tags><a href=https://learnbyexample.github.io/tags/vim/>#vim</a><a href=https://learnbyexample.github.io/tags/tip/>#tip</a></div><hr color=#e6e6e6><div class=post-nav><p><a class=next href=https://learnbyexample.github.io/tips/vim-tip-28/>Vim tip 28: miscellaneous motion and reposition commands →</a><br></div><hr color=#e6e6e6><p>📰 Use <a href=https://learnbyexample.github.io/atom.xml>this link</a> for the Atom feed. <br> ✅ Follow me on <a href=https://twitter.com/learn_byexample>Twitter</a>, <a href=https://github.com/learnbyexample>GitHub</a> and <a href=https://www.youtube.com/c/learnbyexample42>Youtube</a> for interesting tech nuggets. <br> 📧 Subscribe to <a href=https://learnbyexample.gumroad.com/l/learnbyexample-weekly>learnbyexample weekly</a> for programming resources, tips, tools, free ebooks and more (free newsletter, delivered every Friday).<hr color=#e6e6e6></div></article></div></main></div><script src=https://learnbyexample.github.io/even.js></script>
0 commit comments