http://qs321.pair.com?node_id=1177010

Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

Hi Monks,

I played with regular expression and perl does something different as I would expect. Only the "+" operator worked as expected. When the "*" or "?" is at the end of a expression, is seems it is sometimes not greedy. Please see my example:

use strict; use warnings; $_="kkkaaabc"; print "String = $_\n"; print "1 $& Expected: ka ?? /k?a?/\n" if /k?a?/; print "2 $& Expected: kkka OK\n" if /k*a?/; print "3 $& Expected: kaaa OK\n" if /k?a+/; print "4 $& Expected: kkkaaa OK\n" if /k+a+/; print "5 $& Expected: kaaa ?? /k?a*/\n" if /k?a*/; print "6 $& Expected: kkkaaa OK\n" if /k*a*/;

The result is:

String = kkkaaabc 1 k Expected: ka ?? /k?a?/ 2 kkka Expected: kkka OK 3 kaaa Expected: kaaa OK 4 kkkaaa Expected: kkkaaa OK 5 k Expected: kaaa ?? /k?a*/ 6 kkkaaa Expected: kkkaaa OK

Why do I get in case 1 and 5 no "a" at the end of the matching circuit ??. Many Thanks for any help !