The forms *?, +?, and ?? provide non-greedy versions of *, +, and ? and have been available since Emacs 21.1, released in 2001.

Emulation with ancient emacsen

In Emacs 21.1 and later versions Perl-like non-greedy syntax (.*?) has been implemented.

Howto get “minimal matching” on a particular \\(.*\\) sub expression, ie. howto write a non-greedy regular expressions as in Perl (.*?):


A strict POSIX implementation will still match the above greedily, but most backtracking implementations will do the above match non-greedily, just as (.*?) would. The above hack works in Emacs versions earlier than 21.1.

In either case the * and + special characters can be used to create a non-greedy regexp.