Why does this code not return what I expect it to?

Heres the code. Why does it miss out the "a" character?

···

------------------------------------------
"This is a test".scan(/\w\w/) {|x| puts x}
------------------------------------------
Thanks
--
Posted via http://www.ruby-forum.com/.

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

your regex mataches 2 letter word characters. "a" is only one.

David Morton
Maia Mailguard http://www.maiamailguard.com
mortonda@dgrmm.net

···

On Jan 3, 2008, at 3:23 PM, Sam Phoneix wrote:

"This is a test".scan(/\w\w/) {|x| puts x}

Have you researched what the \w matches in a regular expression, and considered how many of them there are in the argument to scan?

Mike

···

On 3-Jan-08, at 4:23 PM, Sam Phoneix wrote:

Heres the code. Why does it miss out the "a" character?
------------------------------------------
"This is a test".scan(/\w\w/) {|x| puts x}
------------------------------------------
Thanks
-- Posted via http://www.ruby-forum.com/.

--

Mike Stok <mike@stok.ca>
http://www.stok.ca/~mike/

The "`Stok' disclaimers" apply.

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

David Morton wrote:

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1
your regex mataches 2 letter word characters. "a" is only one.
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.7 (Darwin)

iD8DBQFHfVNzUy30ODPkzl0RAucoAJ45N5rkzbbuMGr3jHAiF1NAwfPLrgCfeYo0
pDib5SU+hWuIJfKirdd9G6s=
=VpNa
-----END PGP SIGNATURE-----

Thanks

···

--
Posted via http://www.ruby-forum.com/.