On Nov 24, 7:06 pm, jwkr...@shaw.ca (John W. Krahn) wrote: > ... > > 1. before the a > > 2. between the a and b > > 3. before the b > > 4. after the b > > > The problem is that the third one should not be there. > > Yes it should. Perhaps you are confused because pos() reports the > current position *after* the match has occured? A zero width match has > the same position at the beginning and end however the 'XXX' string has > a different position at the beginning and end. Try using $-[0] for the > beginning of the match and $+[0] for the end of the match. > > $ perl -le '$_="aXXXb"; print "one word is $1 at $-[0] to $+[0]" while > /(X*)/g;' > one word is at 0 to 0 > one word is XXX at 1 to 4 > one word is at 4 to 4 > one word is at 5 to 5 >
The match, pre-match, post-match captures can be helpful at times IMO to see what's going on: $_="aXXXb"; print "one word is ", ( $& eq ''" ? "empty " : " $& "), "pre:$` post:$' " while /(X*)/gp; Or, using 5.10.0 to avoid the $&, $`, $' penalty: print "one word is ", ( ${^MATCH} eq '"" ? "empty " : "${^MATCH} "), "pre:${^PREMATCH} post:${^POSTMATCH}" while /(X*)/gp; one word is empty pre: post:aXXXb one word is XXX pre:a post:b one word is empty pre:aXXX post:b one word is empty pre:aXXXb post: -- Charles DeRykus -- To unsubscribe, e-mail: beginners-unsubscr...@perl.org For additional commands, e-mail: beginners-h...@perl.org http://learn.perl.org/