On May 30, 10:02 am, [EMAIL PROTECTED] (Chas Owens) wrote: > On 5/30/07, Sharan Basappa <[EMAIL PROTECTED]> wrote:
> > You mention that if I write a rule like @store = $str =~ m/((?=\d\d\d))/g; > > then the scanner does not move ahead. But as I mentioned in my mail, > > the result of this regex is 123 234 etc. This clearly shows that after every > > match, > > the regex engine of perl is moving its pointer to next char in the string > > (i.e. it starts > > looking at 23456 once 123 is matched) > > This was exactly my question. > > Because it always moves ahead by either one character or the match, > but zero-width constructs do not consume any characters. That is why > they are called zero-width. I got confused by this too. I think Sharan's question comes down to "why isn't this an infinite loop?" That is, why does pos() move ahead one character when it matches 0 characters? This is not limited to look-ahead assertions. The behavior can be seen in other constructs as well. For example: $ perl -wle' $string = "abc"; while ($string =~ /(.*?)/g) { print pos($string), ": ", $1; } ' 0: 1: a 1: 2: b 2: 3: c 3: It appears that Perl is actually dividing the string up into "characters" and "slots between character", and allowing pos() to move to each of them in sequence. So at the beginning, it's at the slot before the first character, and it can successfully match 0 characters. Then pos() moves to the first character, and the fewest characters it can find is that one character, so $1 gets 'a'. Then it moves to the slot between 'a' and 'b'. Etc. Here's another, that doesn't allow any characters to be matched: $ perl -wle' $string = "abc"; while ($string =~ /(.{0})/g) { print pos($string), ": ", $1; } ' 0: 1: 2: 3: Would the above be an accurate description of what's happening? And if so, is this behavior documented anywhere? I couldn't find it in a cursory examanation of either perlop or perlre... Thanks, Paul Lalli -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/