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

Alle giovedì 3 gennaio 2008, Sam P. 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

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

On Jan 3, 2008, at 3:23 PM, Sam P. wrote:

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

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

David M.
Maia Mailguard http://www.maiamailguard.com
[email protected]

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.7 (Darwin)

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

On 3-Jan-08, at 4:23 PM, Sam P. 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/.

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

Mike S. [email protected]
http://www.stok.ca/~mike/

The “`Stok’ disclaimers” apply.

David M. 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

Alle giovedì 3 gennaio 2008, Stefano C. ha scritto:

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