--1926193751-791138259-1254076359840
Content-Type: MULTIPART/MIXED; BOUNDARY="1926193751-791138259-1254076359=:3840"

  This message is in MIME format.  The first part should be readable text,
  while the remaining parts are likely unreadable without MIME-aware tools.

--1926193751-791138259-1254076359840
Content-Type: TEXT/PLAIN; charset=utf-8; format=flowed
Content-Transfer-Encoding: 8BIT

Hi --

On Mon, 28 Sep 2009, Jim Burgess wrote:

> Vielen Dank für deine Antwort :-)
> I was unaware that the reg ex would start from the point where it
> encounters the first character satisfying \w.

The pattern will always try to match as far to the left in the string
as possible. Greediness or lack thereof pertains only to how far right
it goes.

Here are some further examples that illustrate some of this:

Note the difference between these two:

>> /x?!/.match("xxxx!")[0]
"x!"
>> /!/.match("xxxx!")[0]
"!"

You might think that in the first one, since x? means zero-or-one of
'x', it would settle for zero and just match "!". But it never gets
that far; it progresses strictly from left to right, reaches the
imaginary position between the third and fourth x's, and asks itself:
am I now in a position to match zero-or-one of 'x' followed by exactly
one '!'? The answer is yes, and the match consumes two characters.
Since the match has succeeded, the process is over; the engine never
has to know that if it had advanced one more character, it would also
have found a match for the same pattern.

Then there's this:

>> /x!?/.match("xxxx!")[0]
"x"

In this case, the regex engine gets to the same point and asks: can I
match exactly one 'x', followed by zero-or-one of '!'? The answer is
yes, and the match only requires one character. Since that one
character fits the pattern, the matching process stops immediately.
You might say that /!?/ is the non-greedy version of /!/ (and I
imagine people have, though I don't know where :-) and greediness is
always about left-to-right progress through the string.

(I am paraphrasing the brain of the regex engine; some of this may or
may not be optimized away in any given implementation.)


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)
--1926193751-791138259-1254076359840--
--1926193751-791138259-1254076359840--