Lazy vs. Greedy RegExes

Since it was briefly touched among my colleagues today, let me share our insights. The question was if a regular expression like (/.*somepath$) would match a path starting with a “/” and ending with “somepath”, or if it would be matching anything after “/”. As we found out, the RegEx is right, it matches paths like /path/to/somepath or simply /somepath. 

There is a related question leading us to to the question of laziness. What if we have an expression like
str.*g

And what if it is followed by ? so that we have 
str.*?g

Greedy Capture

Here’s an example: looking for
mou.* 
the search starts at the first occurence of mouseX, ending at the end of the line (the dot doesn’t match newline). This is a greedy capture.

Lazy Capture

But if we are using the expression
mou.*?

we can see that the capture stops right after the first occurence of mou. It’s taking the shortest match: this is a lazy capture.

About Manfred Berndtgen

Manfred Berndtgen, maintainer of this site, is a part-time researcher with enough spare time for doing useless things and sharing them with the rest of the world. His main photographic subjects are made of plants or stones, and since he's learning Haskell everything seems functional to him.