Hi --

On Mon, 28 Sep 2009, Jim Burgess wrote:

> Why does:
> puts /\w+? \w+?/.match "Ein kleiner Satz"
> return "Ein k" and not "n k"
> My understanding was that \w represents any alphanumerical character, +
> means that it occurs at least once and the ? following the plus makes it
> non-greedy.
> I would have thought this returned "n k" although that is obviously not
> correct.

Eine kleine Misverstaendnis :-)

\w+ means "one or more from the set (alphanumeric + underscore)".
The pattern will start trying to match at the beginning of the string,
where it finds \w+, then the space, then more \w+. It grabs all of Ein
because it hasn't found a space yet (but it also hasn't found any
reason to stop proceeding along the string, since "E", "Ei", and "Ein"
are all \w+). It only grabs the k in kleiner because that satisfies
the pattern in non-greedy fashion.

In this particular case, the first + doesn't matter, because the
pattern will keep going until it finds a space, so normal rules of
(non-)greediness don't apply.


David

-- 
David A. Black, Director
Ruby Power and Light, LLC (http://www.rubypal.com)
Ruby/Rails training, consulting, mentoring, code review
Book: The Well-Grounded Rubyist (http://www.manning.com/black2)