tags:

views:

195

answers:

5

could someone explain these 2 terms in a understandable way?

+4  A: 

Greedy means your expression will match as large a group as possible, lazy means it will match the smallest group possible. For this string:

abcdefghijklmc

and this expression:

a.*c

A greedy match will match the whole string, and a lazy match will match just the first abc.

Carl Norum
+1 http://www.regular-expressions.info/repeat.html
Jonathan Sampson
Did you mean `a.*c`?
Laurence Gonsalves
@Laurence, yup.
Carl Norum
+2  A: 

Greedy means match longest possible string.

Lazy means match shortest possible string.

For example, the greedy h.+l matches 'hell' in 'hello' but the lazy h.+?l matches 'hel'.

slebetman
i got it now! excellent!
weng
+2  A: 

From Regular expression

The standard quantifiers in regular expressions are greedy, meaning they match as much as they can, only giving back as necessary to match the remainder of the regex.

By using a lazy quantifier, the expression tries the minimal match first.

astander
+4  A: 

Greedy will consume as much as possible. From http://www.regular-expressions.info/repeat.html we see the example of trying to match HTML tags with <.+>. Suppose you have the following:

<em>Hello World</em>

You may think that <.+> (. means anything and + means repeated) would only match the <em> and the </em>, when in reality it will be very greedy, and go from the first < to the last >. This means it will match <em>Hello World</em> instead of what you wanted.

Making it lazy (<.+?>) will prevent this. By adding the ? after the +, we tell it to repeat as few times as possible, so the first > it comes across, is where we want to stop the matching.

I'd encourage you to download RegExr, a great tool that will help you explore Regular Expressions - I use it all the time.

Jonathan Sampson
so if you use greedy will u have 3 (1 element + 2 tags) matches or just 1 match (1 element)?
weng
It would match only 1 time, starting from the first **<** and ending with the last **>**.
Jonathan Sampson
But making it lazy would match twice, giving us both the opening and closing tag, ignoring the text in between (since it doesn't fit the expression).
Jonathan Sampson
A: 

Для Hello World: /(?<=).+(?=<\/em>)/ Результат Hello World

paladi sergey