History always repeats itself and sometimes that's good. Here's a second chance to be part of it: tmdtc.com

Friday 23 May 2008

Vim: Shortest match of your search pattern

I just discovered that if a "-" appears immediately after the "{", then a shortest match first algorithm is used. In particular, "\{-}" is the same as "*" but uses the shortest match first algorithm.

An example from http://www.ph.unimelb.edu.au/~ssk/vim/pattern.html:
a[bc]\{-}[cd] matches "abc" in "abcd"
a[bc]*[cd] matches "abcd" in "abcd"

Note however that a match that starts earlier is preferred over a shorter match:
"a\{-}b" matches "aaab" in "xaaab".

1 comment:

Anonymous said...

Very useful.