| Subject: | Re: a match question |
|---|---|
| From: | John W. Krahn |
| Date: | Tue, 24 Nov 2009 19:06:56 -0800 |
| Newsgroups: | perl.beginners |
Shawn H Corey wrote: Jim Gibson wrote:On 11/24/09 Tue Nov 24, 2009 4:42 PM, "Orchid Fairy (À¼»¨ÏÉ×Ó)" <practicalperl@xxxxxxxxx> scribbled:On Wed, Nov 25, 2009 at 8:38 AM, Shawn H Corey <shawnhcorey@xxxxxxxxx> wrote:It seems to be picking up an extra empty string after a non-zero length match.Thanks John and Shawn. Yup what let me be confused is that, why there is an additional empty string there?You get 4 matches because there are four places in the string 'aXXXb' that can be matched by 'X*'. Note that a "place" in this case is a position between characters. The places are: 1. before the a 2. after the a 3. before the b 4. after the bFunny, I get this on my machine: $ perl -le '$_="aXXXb"; print "one word is $1 at ", pos while(/(X*)/g);' one word is at 0 one word is XXX at 4 one word is at 4 one word is at 5 That is: 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 John -- The programmer is fighting against the two most destructive forces in the universe: entropy and human stupidity. -- Damian Conway |
| <Prev in Thread] | Current Thread | [Next in Thread> |
|---|---|---|
| ||
| Previous by Date: | Re: a match question, John W. Krahn |
|---|---|
| Next by Date: | Re: a match question, practicalperl |
| Previous by Thread: | Re: a match question, Shawn H Corey |
| Next by Thread: | Re: a match question, practicalperl |
| Indexes: | [Date] [Thread] [Top] [All Lists] |