<?xml version="1.0" encoding="windows-1252"?>
<node id="859295" title="Re: Greediness of * vs. greediness of +" created="2010-09-08 05:29:56" updated="2010-09-08 05:29:56">
<type id="11">
note</type>
<author id="616540">
moritz</author>
<data>
<field name="doctext">
The overriding principle of the regex engine is to try the left-most possible match first.

&lt;p&gt;In the case of b*, it matches zero times at the start of the string (ie before the first character) - which is as many as it can, because there are not more than 0 leading b's in your string.

&lt;p&gt;In the case of b+, the empty match is not successful, so the regex engine bumps along, and tries to match as many b's as possible, starting from the second position (ie string index 1).

&lt;p&gt;So both are greedy, but neither do a search for longest match of consecutive b's. try

&lt;code&gt;
$ perl -lwe '"abbabbbbc"=~/(b+)/ &amp;&amp; print "Found: $1"'
Found: bb
&lt;/code&gt;

&lt;p&gt;It used the first position where b+ matched, and there took as many b's as possible.
&lt;div class="pmsig"&gt;&lt;div class="pmsig-616540"&gt;
[http://perl6.org/|Perl 6] - links to (nearly) everything that is Perl 6.
&lt;/div&gt;&lt;/div&gt;</field>
<field name="root_node">
859294</field>
<field name="parent_node">
859294</field>
</data>
</node>
