From: Stefano Crocco Date: 2008-01-04T06:34:30+09:00 Subject: Re: Why does this code not return what I expect it to? Alle giovedì 3 gennaio 2008, Stefano Crocco ha scritto: > Alle giovedì 3 gennaio 2008, Sam Phoneix ha scritto: > > Heres the code. Why does it miss out the "a" character? > > ------------------------------------------ > > "This is a test".scan(/\w\w/) {|x| puts x} > > ------------------------------------------ > > Thanks > > I guess because the space before the 'a' is not a word character, so ' a' > can't match /\w\w/. > > Stefano Actually, the situation is a little more complex than I first thought, because there are other characters near spaces which are included in the result. The difference comes from the fact that 'a' has a space before and a space after it. The character 'i' of 'it', instead, is printed because ' i' doesn't match, but 'it' does. With the 'a', there is no matching: neither ' a' nor 'a ' match. The same happens for each word containing an odd number of characters. For instance, replacing 'is' with 'isx', you don't get the 'x' in the output. Stefano