Thursday, January 15, 2009

Greediness

Greediness

As you might have seen the wildcard behaves greedy.

Referring to my last post we had the regexp

t.+l

This text is all about the complicated stuff.

There is an l in all , but the greedy wildcard with the repetition matches up to the l in complicated.

To make your match non-greedy you simply put a ? after the + or *

t.+?l

Now you can see, that the regular expression matches two times.

This text is all about the complicated stuff.

This is very important because in the most cases you might wish to match the first occurrence
of something rather than the last.

No comments:

Post a Comment