# Vim tip 29: greedy quantifiers

Quantifiers can be applied to literal characters, dot metacharacter, groups, backreferences and character classes.

`*`

match zero or more times`abc*`

matches`ab`

or`abc`

or`abccc`

or`abcccccc`

but not`bc`

`Error.*valid`

matches`Error: invalid input`

but not`valid Error`

`s/a.*b/X/`

replaces`table bottle bus`

with`tXus`

since`a.*b`

matches from the first`a`

to the last`b`

`\+`

match one or more times`abc\+`

matches`abc`

or`abccc`

but not`ab`

or`bc`

`\?`

match zero or one times`\=`

can also be used, helpful if you are searching backwards with the`?`

command`abc\?`

matches`ab`

or`abc`

. This will match`abccc`

or`abcccccc`

as well, but only the`abc`

portion`s/abc\?/X/`

replaces`abcc`

with`Xc`

`\{m,n}`

match`m`

to`n`

times (inclusive)`ab\{1,4}c`

matches`abc`

or`abbc`

or`xabbbcz`

but not`ac`

or`abbbbbc`

`\{m,}`

match at least`m`

times`ab\{3,}c`

matches`xabbbcz`

or`abbbbbc`

but not`ac`

or`abc`

or`abbc`

`\{,n}`

match up to`n`

times (including`0`

times)`ab\{,2}c`

matches`abc`

or`ac`

or`abbc`

but not`xabbbcz`

or`abbbbbc`

`\{n}`

match exactly`n`

times`ab\{3}c`

matches`xabbbcz`

but not`abbc`

or`abbbbbc`

Greedy quantifiers will consume as *much* as possible, provided the overall pattern is also matched. That's how the `Error.*valid`

example worked. If `.*`

had consumed everything after `Error`

, there wouldn't be any more characters to try to match `valid`

. How the regexp engine handles matching varying amount of characters depends on the implementation details (backtracking, NFA, etc).

See :h pattern-overview for more details.

If you are familiar with other regular expression flavors like Perl, Python, etc, you'd be surprised by the use of `\`

in the above examples. If you use `\v`

very magic modifier, the `\`

won't be needed.

**Video demo**:

See also my Vim Reference Guide and curated list of resources for Vim.